home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / graphics / gnuplot / command.c < prev    next >
C/C++ Source or Header  |  1993-09-15  |  117KB  |  4,332 lines

  1. #ifndef lint
  2. static char    *RCSid = "$Id: command.c%v 3.50.1.15 1993/08/21 15:23:42 woo Exp $";
  3. #endif
  4.  
  5.  
  6. /* GNUPLOT - command.c */
  7. /*
  8.  * Copyright (C) 1986 - 1993   Thomas Williams, Colin Kelley
  9.  * 
  10.  * Permission to use, copy, and distribute this software and its documentation
  11.  * for any purpose with or without fee is hereby granted, provided that the
  12.  * above copyright notice appear in all copies and that both that copyright
  13.  * notice and this permission notice appear in supporting documentation.
  14.  * 
  15.  * Permission to modify the software is granted, but not the right to distribute
  16.  * the modified code.  Modifications are to be distributed as patches to
  17.  * released version.
  18.  * 
  19.  * This software is provided "as is" without express or implied warranty.
  20.  * 
  21.  * 
  22.  * AUTHORS
  23.  * 
  24.  * Original Software: Thomas Williams,  Colin Kelley.
  25.  * 
  26.  * Gnuplot 2.0 additions: Russell Lang, Dave Kotz, John Campbell.
  27.  * 
  28.  * Gnuplot 3.0 additions: Gershon Elber and many others.
  29.  * 
  30.  * Changes:
  31.  * 
  32.  * Feb 5, 1992    Jack Veenstra    (veenstra@cs.rochester.edu) Added support to
  33.  * filter data values read from a file through a user-defined function before
  34.  * plotting. The keyword "thru" was added to the "plot" command. Example
  35.  * syntax: f(x) = x / 100 plot "test.data" thru f(x) This example divides all
  36.  * the y values by 100 before plotting. The filter function processes the
  37.  * data before any log-scaling occurs. This capability should be generalized
  38.  * to filter x values as well and a similar feature should be added to the
  39.  * "splot" command.
  40.  * 
  41.  * 19 September 1992  Lawrence Crowl  (crowl@cs.orst.edu)
  42.  * Added user-specified bases for log scaling.
  43.  * 
  44.  * There is a mailing list for gnuplot users. Note, however, that the
  45.  * newsgroup 
  46.  *    comp.graphics.gnuplot 
  47.  * is identical to the mailing list (they
  48.  * both carry the same set of messages). We prefer that you read the
  49.  * messages through that newsgroup, to subscribing to the mailing list.
  50.  * (If you can read that newsgroup, and are already on the mailing list,
  51.  * please send a message info-gnuplot-request@dartmouth.edu, asking to be
  52.  * removed from the mailing list.)
  53.  *
  54.  * The address for mailing to list members is
  55.  *       info-gnuplot@dartmouth.edu
  56.  * and for mailing administrative requests is 
  57.  *       info-gnuplot-request@dartmouth.edu
  58.  * The mailing list for bug reports is 
  59.  *       bug-gnuplot@dartmouth.edu
  60.  * The list of those interested in beta-test versions is
  61.  *       info-gnuplot-beta@dartmouth.edu
  62.  */
  63.  
  64. #include <stdio.h>
  65. #include <math.h>
  66. #include <ctype.h>
  67.  
  68. #ifdef AMIGA_AC_5
  69. #include <time.h>
  70. void            sleep();    /* defined later */
  71. #endif
  72.  
  73. #ifdef OS2
  74. #include <setjmp.h>
  75. extern jmp_buf env;       /* from plot.c */
  76. #endif
  77.  
  78. #if defined(MSDOS) || defined(DOS386)
  79. #ifdef DJGPP
  80. #include <dos.h>
  81. #else
  82. #include <process.h>
  83. #endif
  84.  
  85. #ifdef __ZTC__
  86. #define P_WAIT 0
  87. #include <time.h>        /* usleep() */
  88. #else
  89.  
  90. #ifdef __TURBOC__
  91. #ifndef _Windows
  92. #include <dos.h>        /* sleep() */
  93. #include <conio.h>
  94. #include <dir.h>    /* setdisk() */
  95. extern unsigned _stklen = 16394;/* increase stack size */
  96. #endif
  97.  
  98. #else                /* must be MSC */
  99. #if !defined(__EMX__) && !defined(DJGPP)
  100. #ifdef __MSC__
  101. #include <direct.h>        /* for _chdrive() */
  102. #endif
  103. #include <time.h>        /* kludge to provide sleep() */
  104. void            sleep();    /* defined later */
  105. #endif                /* !__EMX__ && !DJGPP */
  106. #endif                /* TURBOC */
  107. #endif                /* ZTC */
  108.  
  109. #endif                /* MSDOS */
  110.  
  111. #ifdef ATARI
  112. #ifdef __PUREC__
  113. #include <ext.h>
  114. #include <tos.h>
  115. #include <aes.h>
  116. #else
  117. #include <osbind.h>
  118. #include <aesbind.h>
  119. #endif /* __PUREC__ */
  120. #endif /* ATARI */
  121.  
  122. #ifdef AMIGA_SC_6_1
  123. #include <proto/dos.h>
  124. void            sleep();
  125. #endif                /* AMIGA_SC_6_1 */
  126.  
  127. #include "plot.h"
  128. #include "setshow.h"
  129. #ifndef _Windows
  130. #include "help.h"
  131. #else
  132. #define MAXSTR 255
  133. #endif
  134.  
  135. #ifndef STDOUT
  136. #define STDOUT 1
  137. #endif
  138.  
  139. #ifndef HELPFILE
  140. #if defined( MSDOS ) || defined( OS2 ) || defined(DOS386)
  141. #define HELPFILE "gnuplot.gih"
  142. #else
  143. #ifdef AMIGA_SC_6_1
  144. #define HELPFILE "S:gnuplot.gih"
  145. #else
  146. #define HELPFILE "docs/gnuplot.gih"    /* changed by makefile */
  147. #endif                /* AMIGA_SC_6_1 */
  148. #endif
  149. #endif                /* HELPFILE */
  150.  
  151. #ifdef _Windows
  152. #include <windows.h>
  153. #include <setjmp.h>
  154. #ifdef __MSC__
  155. #include <malloc.h>
  156. #else
  157. #include <alloc.h>
  158. #include <dir.h>    /* setdisk() */
  159. #endif
  160. #include "win/wgnuplib.h"
  161. void sleep();
  162. extern TW textwin;
  163. extern jmp_buf FAR env;       /* from plot.c */
  164. extern LPSTR winhelpname;
  165. extern void screen_dump(void);    /* in term/win.trm */
  166. extern int Pause(LPSTR mess); /* in winmain.c */
  167. #endif
  168.  
  169. #define inrange(z,min,max) ((min<max) ? ((z>=min)&&(z<=max)) : ((z>=max)&&(z<=min)) )
  170.  
  171. /*
  172.  * instead of <strings.h>
  173.  */
  174.  
  175. #ifndef ATARI
  176. #ifdef _Windows
  177. #include <string.h>
  178. #include <stdlib.h>
  179. #else
  180. #ifndef AMIGA_SC_6_1
  181. extern char    *gets(), *getenv();
  182. #ifdef sequent
  183. extern char    *strcpy(), *strncpy(), *strcat(), *index();
  184. #else
  185. extern char    *strcpy(), *strncpy(), *strcat(), *strchr();
  186. #endif
  187. extern int      strlen();
  188. extern int      strcmp();
  189. extern double   atof();
  190. #endif /* !AMIGA_SC_6_1 */
  191. #endif
  192. #else
  193. #ifdef __PUREC__
  194. /*
  195.  * a substitute for PureC's buggy sscanf.
  196.  * this uses the normal sscanf and fixes the following bugs:
  197.  * - whitespace in format matches whitespace in string, but doesn't
  198.  *   require any. ( "%f , %f" scans "1,2" correctly )
  199.  * - the ignore value feature works (*). this created an address error
  200.  *   in PureC.
  201.  */
  202.  
  203. #include <stdarg.h>
  204. #include <string.h>
  205.  
  206. int purec_sscanf( const char *string, const char *format, ... )
  207. {
  208.   va_list args;
  209.   int cnt=0;
  210.   char onefmt[256];
  211.   char buffer[256];
  212.   const char *f=format;
  213.   const char *s=string;
  214.   char *f2;
  215.   char ch;
  216.   int ignore;
  217.   void *p;
  218.   int *ip;
  219.   int pos;
  220.  
  221.   va_start(args,format);
  222.   while( *f && *s ) {
  223.     ch=*f++;
  224.     if( ch!='%' ) {
  225.       if(isspace(ch)) {
  226.         /* match any number of whitespace */
  227.         while(isspace(*s)) s++;
  228.       } else {
  229.         /* match exactly the character ch */
  230.         if( *s!=ch ) goto finish;
  231.         s++;
  232.       }
  233.     } else {
  234.       /* we have got a '%' */
  235.       ch=*f++;
  236.       if( ch=='%' ) {
  237.         /* match exactly % */
  238.         if( *s!=ch ) goto finish;
  239.         s++;
  240.       } else {
  241.         f2=onefmt;
  242.         *f2++='%';
  243.         *f2++=ch;
  244.         ignore=0;
  245.         if( ch=='*' ) {
  246.           ignore=1;
  247.           ch=f2[-1]=*f++;
  248.         }
  249.         while( isdigit(ch) ) {
  250.           ch=*f2++=*f++;
  251.         }
  252.         if( ch=='l' || ch=='L' || ch=='h' ) {
  253.           ch=*f2++=*f++;
  254.         }
  255.         switch(ch) {
  256.           case '[':
  257.             while( ch && ch!=']' ) {
  258.               ch=*f2++=*f++;
  259.             }
  260.             if( !ch ) goto error;
  261.             break;
  262.           case 'e':
  263.           case 'f':
  264.           case 'g':
  265.           case 'd':
  266.           case 'o':
  267.           case 'i':
  268.           case 'u':
  269.           case 'x':
  270.           case 'c':
  271.           case 's':
  272.           case 'p':
  273.           case 'n': /* special case handled below */
  274.             break;
  275.           default:
  276.             goto error;
  277.         }
  278.         if( ch!='n' ) {
  279.           strcpy(f2,"%n");
  280.           if( ignore ) {
  281.             p=buffer;
  282.           } else {
  283.             p=va_arg(args,void *);
  284.           }
  285.           switch( sscanf( s, onefmt, p, &pos ) ) {
  286.             case EOF: goto error;
  287.             case  0 : goto finish;
  288.           }
  289.           if( !ignore ) cnt++;
  290.           s+=pos;
  291.         } else {
  292.           if( !ignore ) {
  293.             ip=va_arg(args,int *);
  294.             *ip=(int)(s-string);
  295.           }
  296.         }
  297.       }
  298.     }
  299.   }
  300.  
  301.   if( !*f ) goto finish;
  302.  
  303. error:
  304.   cnt=EOF;
  305. finish:
  306.   va_end(args);
  307.   return cnt;
  308. }
  309.  
  310. /* use the substitute now. I know this is dirty trick, but it works. */
  311. #define sscanf purec_sscanf
  312.  
  313. #endif /* __PUREC__ */
  314. #endif /* ATARI */
  315.  
  316. /*
  317.  * Only reference to contours library.
  318.  */
  319. extern struct gnuplot_contours *contour();
  320.  
  321. #ifdef OS2
  322.  /* emx has getcwd, chdir that can handle drive names */
  323. #define getcwd _getcwd2
  324. #define chdir  _chdir2
  325. #endif /* OS2 */
  326.  
  327. #if defined(unix) && !defined(hpux)
  328. #ifdef GETCWD
  329. extern char    *getcwd();    /* some Unix's use getcwd */
  330. #else
  331. extern char    *getwd();    /* most Unix's use getwd */
  332. #endif
  333. #else
  334. #ifdef DJGPP
  335. extern char    *getwd();    /* DJGPP acts like Unix here */
  336. #else
  337. extern char    *getcwd();    /* Turbo C, MSC, EMX, OS2 and VMS use getcwd */
  338. #endif
  339. #endif
  340.  
  341. #ifdef vms
  342. int             vms_vkid;    /* Virtual keyboard id */
  343. #endif
  344.     
  345. static FILE *data_fp=NULL;    /* != means file still open */
  346. static TBOOLEAN more_data_fp=FALSE;  /* And this explicitly says so. */
  347.  
  348. #if defined(unix) || defined(PIPES)
  349. extern FILE    *popen();
  350. static TBOOLEAN  pipe_open = FALSE;
  351. #endif
  352.  
  353. extern int      chdir();
  354.  
  355. extern double   magnitude(), angle(), real(), imag();
  356. extern struct value *const_express(), *pop(), *Gcomplex();
  357. extern struct at_type *temp_at(), *perm_at();
  358. extern struct udft_entry *add_udf();
  359. extern struct udvt_entry *add_udv();
  360. extern void     squash_spaces();
  361. extern void     lower_case();
  362.  
  363. /* local functions */
  364. static enum coord_type adjustlog();
  365.  
  366. extern TBOOLEAN  interactive;    /* from plot.c */
  367.  
  368. /* input data, parsing variables */
  369. struct lexical_unit token[MAX_TOKENS];
  370. char            input_line[MAX_LINE_LEN + 1] = "";
  371. int             num_tokens, c_token;
  372. int             inline_num = 0;    /* input line number */
  373.  
  374. char            c_dummy_var[MAX_NUM_VAR][MAX_ID_LEN + 1];    /* current dummy vars */
  375.  
  376. /* the curves/surfaces of the plot */
  377. struct curve_points *first_plot = NULL;
  378. struct surface_points *first_3dplot = NULL;
  379. static struct udft_entry plot_func;
  380. struct udft_entry *dummy_func;
  381.  
  382. /* jev -- for passing data thru user-defined function */
  383. static struct udft_entry ydata_func;
  384.  
  385. /* support for replot command */
  386. char            replot_line[MAX_LINE_LEN + 1] = "";
  387. static int      plot_token;    /* start of 'plot' command */
  388.  
  389. /* If last plot was a 3d one. */
  390. TBOOLEAN         is_3d_plot = FALSE;
  391.  
  392. com_line()
  393. {
  394.    if (read_line(PROMPT))
  395.        return(1);
  396.  
  397.     /* So we can flag any new output: if false at time of error, */
  398.     /* we reprint the command line before printing caret. */
  399.     /* TRUE for interactive terminals, since the command line is typed. */
  400.     /* FALSE for non-terminal stdin, so command line is printed anyway. */
  401.     /* (DFK 11/89) */
  402.     screen_ok = interactive;
  403.  
  404.     if (do_line())
  405.         return(1);
  406.      else
  407.         return(0);
  408. }
  409.  
  410.  
  411. do_line()
  412. {                /* also used in load_file */
  413.     if (is_system(input_line[0])) {
  414.     do_system();
  415.     (void) fputs("!\n", stderr);
  416.     return(0);
  417.     }
  418.     num_tokens = scanner(input_line);
  419.     c_token = 0;
  420.     while (c_token < num_tokens) {
  421.     if (command())
  422.            return(1);
  423.     if (c_token < num_tokens)    /* something after command */
  424.         if (equals(c_token, ";"))
  425.         c_token++;
  426.         else
  427.         int_error("';' expected", c_token);
  428.     }
  429.     return(0);
  430. }
  431.  
  432.  
  433.  
  434. command()
  435. {
  436.     FILE *fp, *lf_top();
  437.     int             i;
  438.     char            sv_file[MAX_LINE_LEN + 1];
  439. #if defined(__ZTC__)
  440.     unsigned dummy; /* it's a parameter needed for dos_setdrive */
  441. #endif
  442.     /* string holding name of save or load file */
  443.  
  444.     for (i = 0; i < MAX_NUM_VAR; i++)
  445.     c_dummy_var[i][0] = '\0';    /* no dummy variables */
  446.  
  447.     if (is_definition(c_token))
  448.     define();
  449.     else if (almost_equals(c_token, "h$elp") || equals(c_token, "?")) {
  450.     c_token++;
  451.     do_help();
  452.     } else if (almost_equals(c_token, "test")) {
  453.     c_token++;
  454.     test_term();
  455.     } else if (almost_equals(c_token, "scr$eendump")) {
  456.     c_token++;
  457. #ifdef _Windows
  458.     screen_dump();
  459. #else
  460.     fputs("screendump not implemented\n",stderr);
  461. #endif
  462.     } else if (almost_equals(c_token, "pa$use")) {
  463.     struct value    a;
  464.     int             stime, text = 0;
  465.     char            buf[MAX_LINE_LEN + 1];
  466.  
  467.     c_token++;
  468.     stime = (int) real(const_express(&a));
  469.     buf[0]='\0';
  470.     if (!(END_OF_COMMAND)) {
  471.         if (!isstring(c_token))
  472.         int_error("expecting string", c_token);
  473.         else {
  474.         quotel_str(buf, c_token);
  475. #ifdef _Windows
  476.         if (stime>=0)
  477. #endif
  478. #ifdef OS2
  479.                 if( strcmp(term_tbl[term].name, "pm" )!=0 || stime >=0 )
  480. #endif
  481.         (void) fprintf(stderr, "%s", buf);
  482.         text = 1;
  483.         }
  484.     }
  485.     if (stime < 0)
  486. #ifdef _Windows
  487.         {
  488.             if (!Pause(buf))
  489.                  longjmp(env, TRUE); /* bail out to command line */
  490.          }
  491. #else
  492. #ifdef OS2
  493.         if( strcmp(term_tbl[term].name, "pm" )==0 && stime < 0 )
  494.         {
  495.             int rc ;
  496.             if( (rc=PM_pause( buf ))==0 ) longjmp(env,TRUE) ;
  497.             else if( rc==2 ) { 
  498.         (void) fprintf(stderr, "%s", buf);
  499.         text = 1;
  500.                 (void) fgets(buf, MAX_LINE_LEN, stdin);
  501.                 }
  502.         }
  503. #else
  504.         (void) fgets(buf, MAX_LINE_LEN, stdin);
  505.     /* Hold until CR hit. */
  506. #endif /*OS2*/
  507. #endif
  508. #ifdef __ZTC__
  509.     if (stime > 0)
  510.         usleep((unsigned long) stime);
  511. #else
  512.     if (stime > 0)
  513.         sleep((unsigned int) stime);
  514. #endif
  515.     if (text != 0 && stime >= 0)
  516.         (void) fprintf(stderr, "\n");
  517.     c_token++;
  518.     screen_ok = FALSE;
  519.     } else if (almost_equals(c_token, "pr$int")) {
  520.     struct value    a;
  521.  
  522.     c_token++;
  523.     (void) const_express(&a);
  524.     (void) putc('\t', stderr);
  525.     disp_value(stderr, &a);
  526.     (void) putc('\n', stderr);
  527.     screen_ok = FALSE;
  528.     } else if (almost_equals(c_token, "p$lot")) {
  529.     plot_token = c_token++;
  530. #ifdef _Windows
  531.     SetCursor(LoadCursor((HINSTANCE)NULL, IDC_WAIT));
  532. #endif
  533.     plotrequest();
  534. #ifdef _Windows
  535.     SetCursor(LoadCursor((HINSTANCE)NULL, IDC_ARROW));
  536. #endif
  537.     } else if (almost_equals(c_token, "sp$lot")) {
  538.     plot_token = c_token++;
  539. #ifdef _Windows
  540.     SetCursor(LoadCursor((HINSTANCE)NULL, IDC_WAIT));
  541. #endif
  542.     plot3drequest();
  543. #ifdef _Windows
  544.     SetCursor(LoadCursor((HINSTANCE)NULL, IDC_ARROW));
  545. #endif
  546.     } else if (almost_equals(c_token, "rep$lot")) {
  547.     if (replot_line[0] == '\0')
  548.         int_error("no previous plot", c_token);
  549.     c_token++;
  550. #ifdef _Windows
  551.     SetCursor(LoadCursor((HINSTANCE)NULL, IDC_WAIT));
  552. #endif
  553.     replotrequest();
  554. #ifdef _Windows
  555.     SetCursor(LoadCursor((HINSTANCE)NULL, IDC_ARROW));
  556. #endif
  557.     } else if (almost_equals(c_token, "se$t"))
  558.     set_command();
  559.     else if (almost_equals(c_token, "sh$ow"))
  560.     show_command();
  561.     else if (almost_equals(c_token, "cl$ear")) {
  562.     if (!term_init) {
  563.         (*term_tbl[term].init) ();
  564.         term_init = TRUE;
  565.     }
  566.     (*term_tbl[term].graphics) ();
  567.     (*term_tbl[term].text) ();
  568.     (void) fflush(outfile);
  569.     screen_ok = FALSE;
  570.     c_token++;
  571.     } else if (almost_equals(c_token, "she$ll")) {
  572.     do_shell();
  573.     screen_ok = FALSE;
  574.     c_token++;
  575.     } else if (almost_equals(c_token, "sa$ve")) {
  576.     if (almost_equals(++c_token, "f$unctions")) {
  577.         if (!isstring(++c_token))
  578.         int_error("expecting filename", c_token);
  579.         else {
  580.         quote_str(sv_file, c_token);
  581.         save_functions(fopen(sv_file, "w"));
  582.         }
  583.     } else if (almost_equals(c_token, "v$ariables")) {
  584.         if (!isstring(++c_token))
  585.         int_error("expecting filename", c_token);
  586.         else {
  587.         quote_str(sv_file, c_token);
  588.         save_variables(fopen(sv_file, "w"));
  589.         }
  590.     } else if (almost_equals(c_token, "s$et")) {
  591.         if (!isstring(++c_token))
  592.         int_error("expecting filename", c_token);
  593.         else {
  594.         quote_str(sv_file, c_token);
  595.         save_set(fopen(sv_file, "w"));
  596.         }
  597.     } else if (isstring(c_token)) {
  598.         quote_str(sv_file, c_token);
  599.         save_all(fopen(sv_file, "w"));
  600.     } else {
  601.         int_error(
  602.              "filename or keyword 'functions', 'variables', or 'set' expected",
  603.              c_token);
  604.     }
  605.     c_token++;
  606.     } else if (almost_equals(c_token, "l$oad")) {
  607.     if (!isstring(++c_token))
  608.         int_error("expecting filename", c_token);
  609.     else {
  610.         quote_str(sv_file, c_token);
  611.         load_file(fp=fopen(sv_file, "r"), sv_file);
  612.         /* input_line[] and token[] now destroyed! */
  613.         c_token = num_tokens = 0;
  614.     }
  615.     } else if (almost_equals(c_token,"rer$ead")) {
  616.             fp = lf_top();
  617.             if (fp != (FILE *)NULL) rewind(fp);
  618.             c_token++;
  619.     } else if (almost_equals(c_token, "cd")) {
  620.     if (!isstring(++c_token))
  621.         int_error("expecting directory name", c_token);
  622.     else {
  623.         quotel_str(sv_file, c_token);
  624. #if defined(MSDOS) || defined(_Windows) || defined(ATARI) || defined(DOS386)
  625.         if (!((strlen(sv_file)==2) && isalpha(sv_file[0]) && (sv_file[1]==':')))
  626. #endif
  627.         if (chdir(sv_file)) {
  628.         int_error("Can't change to this directory", c_token);
  629.         }
  630. #if defined(MSDOS) || defined(_Windows) || defined(ATARI) || defined(DOS386)
  631.         if (isalpha(sv_file[0]) && (sv_file[1]==':')) {
  632. #ifdef ATARI
  633.         (void)Dsetdrv(toupper(sv_file[0])-'A');
  634. #endif
  635.  
  636. #if defined(__ZTC__)
  637.       (void)dos_setdrive(toupper(sv_file[0]) - 'A' + 1, &dummy);
  638. #endif
  639.  
  640. #if defined(MSDOS) && defined(__EMX__)
  641.         (void)_chdrive(toupper(sv_file[0]));
  642. #endif
  643. #if defined(__MSC__)
  644.         (void)_chdrive(toupper(sv_file[0])-'A');
  645. #endif
  646. #if (defined(MSDOS) || defined(_Windows)) && defined(__TURBOC__)
  647.         (void) setdisk(toupper(sv_file[0])-'A');
  648. #endif
  649. #ifdef DJGPP
  650.         { union REGS r;
  651.             r.h.ah = 0x0e;
  652.             r.x.dx = toupper(sv_file[0])-'A';
  653.             intdos(&r, &r);
  654.         }
  655. #endif
  656.         }
  657. #endif
  658.         c_token++;
  659.     }
  660.     } else if (almost_equals(c_token, "pwd")) {
  661. #if defined(unix) && !defined(hpux)
  662. #ifdef GETCWD
  663.     (void) getcwd(sv_file, MAX_ID_LEN);    /* some Unix's use getcwd */
  664. #else
  665.     (void) getwd(sv_file);    /* most Unix's use getwd */
  666. #endif
  667. #else
  668. #ifdef __EMX__
  669.     (void) _getcwd2(sv_file, MAX_ID_LEN);
  670. #else
  671.     /* Turbo C and VMS have getcwd() */
  672.     (void) getcwd(sv_file, MAX_ID_LEN);
  673. #endif
  674. #endif
  675. #ifdef DJGPP
  676.     { union REGS r;
  677.         r.h.ah = 0x19;
  678.         intdos(&r, &r);
  679.         fprintf(stderr, "%c:", r.h.al + 'a');
  680.     }
  681. #endif
  682.     fprintf(stderr, "%s\n", sv_file);
  683.     c_token++;
  684.     } else if (almost_equals(c_token, "ex$it") ||
  685.            almost_equals(c_token, "q$uit")) {
  686.     return(1);
  687.     } else if (!equals(c_token, ";")) {    /* null statement */
  688.     int_error("invalid command", c_token);
  689.     }
  690.     return(0);
  691. }
  692.  
  693. replotrequest()
  694. {
  695.     char            str[MAX_LINE_LEN + 1];
  696.     if (equals(c_token, "["))
  697.     int_error("cannot set range with replot", c_token);
  698.     if (!END_OF_COMMAND) {
  699.     capture(str, c_token, num_tokens - 1);
  700.     if ((strlen(str) + strlen(replot_line)) <= MAX_LINE_LEN - 1) {
  701.         (void) strcat(replot_line, ",");
  702.         (void) strcat(replot_line, str);
  703.     } else {
  704.         int_error("plot line too long with replot arguments", c_token);
  705.     }
  706.     }
  707.     (void) strcpy(input_line, replot_line);
  708.     screen_ok = FALSE;
  709.     num_tokens = scanner(input_line);
  710.     c_token = 1;        /* skip the 'plot' part */
  711.     is_3d_plot ? plot3drequest() : plotrequest();
  712. }
  713.  
  714.  
  715. plotrequest()
  716. /*
  717.  * In the parametric case we can say plot [a= -4:4] [-2:2] [-1:1] sin(a),a**2
  718.  * while in the non-parametric case we would say only plot [b= -2:2] [-1:1]
  719.  * sin(b)
  720.  */
  721. {
  722.     TBOOLEAN         changed;
  723.     int             dummy_token = -1;
  724.  
  725.     is_3d_plot = FALSE;
  726.  
  727.     if (parametric && strcmp(dummy_var[0], "u") == 0)
  728.     strcpy(dummy_var[0], "t");
  729.  
  730.     autoscale_lt = autoscale_t;
  731.     autoscale_lx = autoscale_x;
  732.     autoscale_ly = autoscale_y;
  733.  
  734.     if (!term)            /* unknown */
  735.     int_error("use 'set term' to set terminal type first", c_token);
  736.  
  737.     if (equals(c_token, "[")) {
  738.     c_token++;
  739.     if (isletter(c_token)) {
  740.         if (equals(c_token + 1, "=")) {
  741.         dummy_token = c_token;
  742.         c_token += 2;
  743.         } else {
  744.         /* oops; probably an expression with a variable. */
  745.         /* Parse it as an xmin expression. */
  746.         /* used to be: int_error("'=' expected",c_token); */
  747.         }
  748.     }
  749.     changed = parametric ? load_range(&tmin, &tmax) : load_range(&xmin, &xmax);
  750.     if (!equals(c_token, "]"))
  751.         int_error("']' expected", c_token);
  752.     c_token++;
  753.     if (changed) {
  754.         if (parametric)
  755.         autoscale_lt = FALSE;
  756.         else
  757.         autoscale_lx = FALSE;
  758.     }
  759.     }
  760.     if (parametric && equals(c_token, "[")) {    /* set optional x ranges */
  761.     c_token++;
  762.     changed = load_range(&xmin, &xmax);
  763.     if (!equals(c_token, "]"))
  764.         int_error("']' expected", c_token);
  765.     c_token++;
  766.     if (changed)
  767.         if(parametric)
  768.             autoscale_lt = FALSE;
  769.         else
  770.                 autoscale_lx = FALSE;
  771.     }
  772.     if (equals(c_token, "[")) {    /* set optional y ranges */
  773.     c_token++;
  774.     changed = load_range(&ymin, &ymax);
  775.     if (!equals(c_token, "]"))
  776.         int_error("']' expected", c_token);
  777.     c_token++;
  778.     if (changed)
  779.         autoscale_ly = FALSE;
  780.     }
  781.     /* use the default dummy variable unless changed */
  782.     if (dummy_token >= 0)
  783.     copy_str(c_dummy_var[0], dummy_token);
  784.     else
  785.     (void) strcpy(c_dummy_var[0], dummy_var[0]);
  786.  
  787.     eval_plots();
  788. }
  789.  
  790. plot3drequest()
  791. /*
  792.  * in the parametric case we would say splot [u= -Pi:Pi] [v= 0:2*Pi] [-1:1]
  793.  * [-1:1] [-1:1] sin(v)*cos(u),sin(v)*cos(u),sin(u) in the non-parametric
  794.  * case we would say only splot [x= -2:2] [y= -5:5] sin(x)*cos(y)
  795.  * 
  796.  */
  797. {
  798.     TBOOLEAN         changed;
  799.     int             dummy_token0 = -1, dummy_token1 = -1;
  800.  
  801.     is_3d_plot = TRUE;
  802.  
  803.     if (parametric && strcmp(dummy_var[0], "t") == 0) {
  804.     strcpy(dummy_var[0], "u");
  805.     strcpy(dummy_var[1], "v");
  806.     }
  807.     autoscale_lx = autoscale_x;
  808.     autoscale_ly = autoscale_y;
  809.     autoscale_lz = autoscale_z;
  810.  
  811.     if (!term)            /* unknown */
  812.     int_error("use 'set term' to set terminal type first", c_token);
  813.  
  814.     if (equals(c_token, "[")) {
  815.     c_token++;
  816.     if (isletter(c_token)) {
  817.         if (equals(c_token + 1, "=")) {
  818.         dummy_token0 = c_token;
  819.         c_token += 2;
  820.         } else {
  821.         /* oops; probably an expression with a variable. */
  822.         /* Parse it as an xmin expression. */
  823.         /* used to be: int_error("'=' expected",c_token); */
  824.         }
  825.     }
  826.     changed = parametric ? load_range(&umin, &umax) : load_range(&xmin, &xmax);
  827.     if (!equals(c_token, "]"))
  828.         int_error("']' expected", c_token);
  829.     c_token++;
  830.     if (changed)
  831.         if(parametric) 
  832.             autoscale_lu = FALSE;
  833.         else
  834.             autoscale_lx = FALSE;
  835.     }
  836.     if (equals(c_token, "[")) {
  837.     c_token++;
  838.     if (isletter(c_token)) {
  839.         if (equals(c_token + 1, "=")) {
  840.         dummy_token1 = c_token;
  841.         c_token += 2;
  842.         } else {
  843.         /* oops; probably an expression with a variable. */
  844.         /* Parse it as an xmin expression. */
  845.         /* used to be: int_error("'=' expected",c_token); */
  846.         }
  847.     }
  848.     changed = parametric ? load_range(&vmin, &vmax) : load_range(&ymin, &ymax);
  849.     if (!equals(c_token, "]"))
  850.         int_error("']' expected", c_token);
  851.     c_token++;
  852.     if (changed)
  853.         if(parametric) 
  854.             autoscale_lv = FALSE;
  855.         else
  856.             autoscale_ly = FALSE;
  857.     }
  858.     if (equals(c_token, "[")) {    /* set optional x (parametric) or z ranges */
  859.     c_token++;
  860.     changed = parametric ? load_range(&xmin, &xmax) : load_range(&zmin, &zmax);
  861.     if (!equals(c_token, "]"))
  862.         int_error("']' expected", c_token);
  863.     c_token++;
  864.     if (changed)
  865.         if(parametric) 
  866.             autoscale_lx = FALSE;
  867.         else
  868.             autoscale_lz = FALSE;
  869.     }
  870.     if (equals(c_token, "[")) {    /* set optional y ranges */
  871.     c_token++;
  872.     changed = load_range(&ymin, &ymax);
  873.     if (!equals(c_token, "]"))
  874.         int_error("']' expected", c_token);
  875.     c_token++;
  876.     if (changed)
  877.         autoscale_ly = FALSE;
  878.     }
  879.     if (equals(c_token, "[")) {    /* set optional z ranges */
  880.     c_token++;
  881.     changed = load_range(&zmin, &zmax);
  882.     if (!equals(c_token, "]"))
  883.         int_error("']' expected", c_token);
  884.     c_token++;
  885.     if (changed)
  886.         autoscale_lz = FALSE;
  887.     }
  888.     /* use the default dummy variable unless changed */
  889.     if (dummy_token0 >= 0)
  890.     copy_str(c_dummy_var[0], dummy_token0);
  891.     else
  892.     (void) strcpy(c_dummy_var[0], dummy_var[0]);
  893.  
  894.     if (dummy_token1 >= 0)
  895.     copy_str(c_dummy_var[1], dummy_token1);
  896.     else
  897.     (void) strcpy(c_dummy_var[1], dummy_var[1]);
  898.  
  899.     eval_3dplots();
  900. }
  901.  
  902.  
  903. define()
  904. {
  905.     register int    start_token;/* the 1st token in the function definition */
  906.     register struct udvt_entry *udv;
  907.     register struct udft_entry *udf;
  908.  
  909.     if (equals(c_token + 1, "(")) {
  910.     /* function ! */
  911.     int             dummy_num = 0;
  912.     start_token = c_token;
  913.     do {
  914.         c_token += 2;    /* skip to the next dummy */
  915.         copy_str(c_dummy_var[dummy_num++], c_token);
  916.     } while (equals(c_token + 1, ",") && (dummy_num < MAX_NUM_VAR));
  917.     if (equals(c_token + 1, ","))
  918.         int_error("function contains too many parameters", c_token + 2);
  919.     c_token += 3;        /* skip (, dummy, ) and = */
  920.     if (END_OF_COMMAND)
  921.         int_error("function definition expected", c_token);
  922.     udf = dummy_func = add_udf(start_token);
  923.     if (udf->at)        /* already a dynamic a.t. there */
  924.         free((char *) udf->at);    /* so free it first */
  925.     if ((udf->at = perm_at()) == (struct at_type *) NULL)
  926.         int_error("not enough memory for function", start_token);
  927.     m_capture(&(udf->definition), start_token, c_token - 1);
  928.     } else {
  929.     /* variable ! */
  930.     start_token = c_token;
  931.     c_token += 2;
  932.     udv = add_udv(start_token);
  933.     (void) const_express(&(udv->udv_value));
  934.     udv->udv_undef = FALSE;
  935.     }
  936. }
  937.  
  938. get_data(this_plot)
  939.     struct curve_points *this_plot;
  940. {
  941.     register int    i, j, l_num, datum;
  942.     int fcol[5], scol[5], ncol[5], prevmin, col;
  943.     char  format[MAX_LINE_LEN + 1], data_file[MAX_LINE_LEN + 1],
  944.           line[MAX_LINE_LEN + 1];
  945.     /* conversion variables */
  946.     int n, m, linestat, using;
  947.     char *s;
  948.     double val[5], v[5];
  949.     float fval[5];    /* for use in sscanf */
  950.  
  951.     /* close forgotten input file (in case of a syntax error) */
  952.     if( data_fp ) {
  953. #if defined(unix) || defined(PIPES)
  954.         if (pipe_open) {
  955.             (void) pclose(data_fp);
  956.             pipe_open = FALSE;
  957.         } else
  958. #endif /* unix || PIPES */
  959.         (void) fclose(data_fp);
  960.         data_fp=NULL;
  961.     }
  962.  
  963.     quotel_str(data_file, c_token);
  964.     this_plot->plot_type = DATA;
  965. /*    if (parametric)
  966.         int_error("Parametric data files not yet implemented", NO_CARET);
  967. */
  968. #if defined(unix) || defined(PIPES)
  969.     if (*data_file == '<') {
  970.         if ((data_fp = popen(data_file + 1, "r")) == (FILE *) NULL)
  971.             os_error("cannot create pipe for data", c_token);
  972.         else
  973.             pipe_open = TRUE;
  974.     } else
  975. #endif /* unix || PIPES */
  976.     if ((data_fp = fopen(data_file, "r")) == (FILE *) NULL)
  977.         os_error("can't open data file", c_token);
  978.  
  979.     format[0] = '\0';
  980.     for (i=0; i<5; i++)
  981.         fcol[i] = i+1;
  982.  
  983.     using = 0;
  984.     c_token++;            /* skip data file name */
  985.  
  986.     /* jev -- support for passing data from file thru user function */
  987.     if (almost_equals(c_token, "thru$")) {
  988.         c_token++;
  989.         if (ydata_func.at)
  990.             free(ydata_func.at);
  991.         dummy_func = &ydata_func;
  992.         ydata_func.at = perm_at();
  993.     } else {
  994.         if (ydata_func.at)
  995.             free(ydata_func.at);
  996.         ydata_func.at = NULL;
  997.     }
  998.  
  999.     if (almost_equals(c_token,"u$sing")) {
  1000.         using = 1;
  1001.         c_token++;      /* skip "using" */
  1002.             
  1003.         if (!END_OF_COMMAND && !isstring(c_token)) {
  1004.             struct value a;
  1005.             for (i=0; i<5; i++)
  1006.                 fcol[i] = -1;
  1007.             fcol[0] = (int)magnitude(const_express(&a));
  1008.             for (i=1; equals(c_token,":") && i<5; i++) {
  1009.                 c_token++;      /* skip ":" */
  1010.                 fcol[i] = (int)magnitude(const_express(&a));
  1011.             }
  1012.         }
  1013.  
  1014.         if (!END_OF_COMMAND && isstring(c_token)) {
  1015.             quotel_str(format, c_token);
  1016.             c_token++;    /* skip format */
  1017.         }
  1018.     }
  1019.     
  1020.     /* sort fcol[] into scol[] removing duplicates */
  1021.     prevmin = 0;
  1022.     for (i=0; i<5; i++) {
  1023.         col = 10000;
  1024.         for (j=0; j<5; j++)
  1025.             if ((fcol[j]>prevmin) && (fcol[j]<col))
  1026.                 col = fcol[j];
  1027.         if (col <10000)
  1028.                prevmin = scol[i] = col;
  1029.         else
  1030.             scol[i] = 0;
  1031.     }
  1032.     /* normalise fcol[] into ncol[] */
  1033.     for (i=0; i<5; i++) {
  1034.         if (fcol[i] > 0)
  1035.             for (j=0; j<5; j++) {
  1036.                 if (fcol[i] == scol[j])
  1037.                     ncol[i] = j+1;
  1038.             }
  1039.         else if (fcol[i] == 0)
  1040.             ncol[i] = 0;
  1041.         else
  1042.             ncol[i] = -1;
  1043.     }
  1044.     /* set col to highest column number */
  1045.     col = 0;
  1046.     for (i=0; i<5; i++) 
  1047.         if (fcol[i]>col) col=fcol[i];
  1048.  
  1049.     l_num = 0;
  1050.     datum = 0;
  1051.     i = 0;
  1052.     while (fgets(line, MAX_LINE_LEN, data_fp) != (char *) NULL) {
  1053.         l_num++;
  1054.         if (is_comment(line[0]))
  1055.             continue;        /* ignore comments */
  1056.         if (i >= this_plot->p_max) {
  1057.             /*
  1058.              * overflow about to occur. Extend size of points[] array. We
  1059.              * either double the size, or add 1000 points, whichever is a
  1060.              * smaller increment. Note i=p_max.
  1061.              */
  1062.             cp_extend(this_plot, i + (i < 1000 ? i : 1000));
  1063.         }
  1064.         if (!line[1]) {        /* is it blank line ? */
  1065.             /* break in data, make next point undefined */
  1066.             this_plot->points[i].type = UNDEFINED;
  1067.             i++;
  1068.             continue;
  1069.         }
  1070.         if (strlen(format) != 0) {
  1071.             /* use old sscanf if a format string was given */
  1072.             m = sscanf(line, format, &fval[0], &fval[1], &fval[2], &fval[3], &fval[4]);
  1073.             for (n=0; n<5; n++)        /* convert floats from sscanf to double */
  1074.                 val[n] = (double)fval[n];
  1075.  
  1076.             for (j=0; j<5 && fcol[j]>=0 && fcol[j]-1<m ; j++)
  1077.                 if (fcol[j])
  1078.                     v[j] = val[fcol[j]-1];
  1079.                 else
  1080.                     v[j] = datum;    /* using 0:n */
  1081.         }
  1082.         else {
  1083.             /* implement our own sscanf that skips lines with invalid data 
  1084.              * if a using statement was given */
  1085.             /* convert the array to its constituents */
  1086.             for(n=0; n<5; n++)            /* wipe the array */
  1087.                 val[n] = 0.0;
  1088.             n=0;                        /* n is column number */
  1089.             m=0;                        /* m is number of values read */
  1090.             linestat = 1;                /* linestat: 1 OK 2 bad value 0 EOL */
  1091.             s = line;
  1092.             while ((linestat == 1) && (n<col)) {
  1093.                 while (isspace(*s)) s++;
  1094.                 if (*s == '\0') {
  1095.                     linestat = 0;
  1096.                     break;
  1097.                 }
  1098.                 n++;
  1099.                 if (n == scol[m]) {
  1100.                     if (isdigit(*s) || *s=='-' || *s=='+' || *s=='.') {
  1101.                         val[m] = atof(s);
  1102.                         m++;
  1103.                     }
  1104.                     else
  1105.                         linestat = 2;    /* abort the line non-digit in req loc */
  1106.                 }
  1107.                 while ((!isspace(*s)) && (*s != '\0')) s++;
  1108.             }
  1109.  
  1110.             if (using && (linestat == 2))
  1111.                 continue;    /* skip lines where a using pattern is present and not met */
  1112.  
  1113.             for (j=0; j<5 && ncol[j]>=0 && ncol[j]-1<m ; j++)
  1114.                 if (ncol[j])
  1115.                     v[j] = val[ncol[j]-1];
  1116.                 else
  1117.                     v[j] = datum;    /* using 0:n */
  1118.         }
  1119.  
  1120.         switch (j) {
  1121.             case 1: {        /* only one number */
  1122.                 /* x is index, assign number to y */
  1123.                 v[1]=v[0];
  1124.                 v[0]=datum;
  1125.                 /* nobreak */
  1126.             }
  1127.             case 2: {        /* x, y */
  1128.                 /* ylow and yhigh are same as y */
  1129.                 datum++;
  1130.                 store2d_point(this_plot, i++, v[0], v[1], v[1], v[1], -1.0);
  1131.                 break;
  1132.             }
  1133.             case 3: {        /* x, y, ydelta */
  1134.                 /* ydelta is in the ylow variable */
  1135.                 datum++;
  1136.                 store2d_point(this_plot, i++, v[0], v[1], v[1]-v[2], v[1]+v[2], -1.0);
  1137.                 break;
  1138.             }
  1139.             case 4: {        /* x, y, ylow, yhigh */
  1140.                 datum++;
  1141.                 store2d_point(this_plot, i++, v[0], v[1], v[2], v[3], -1.0);
  1142.                 break;
  1143.             }
  1144.             case 5: {        /* x, y, ylow, yhigh, width */
  1145.                 datum++;
  1146.                 store2d_point(this_plot, i++, v[0], v[1], v[2], v[3], v[4]);
  1147.                 break;
  1148.             }
  1149.             default: {
  1150.                 (void) sprintf(line, "bad data on line %d", l_num);
  1151.                 /* close file before exiting to command level */
  1152. #if defined(unix) || defined(PIPES)
  1153.                 if (pipe_open) {
  1154.                     (void) pclose(data_fp);
  1155.                     pipe_open = FALSE;
  1156.                 } else
  1157. #endif /* unix || PIPES */
  1158.                     (void) fclose(data_fp);
  1159.                 data_fp=NULL;
  1160.                 int_error(line, c_token);
  1161.             }
  1162.         }
  1163.     }
  1164.     this_plot->p_count = i;
  1165.     cp_extend(this_plot, i);    /* shrink to fit */
  1166.  
  1167. #if defined(unix) || defined(PIPES)
  1168.     if (pipe_open) {
  1169.         (void) pclose(data_fp);
  1170.         pipe_open = FALSE;
  1171.     } else
  1172. #endif /* unix || PIPES */
  1173.     (void) fclose(data_fp);
  1174.     data_fp=NULL;
  1175. }
  1176.  
  1177.  
  1178. /* called by get_data for each point */
  1179. store2d_point(this_plot, i, x, y, ylow, yhigh, width)
  1180.     struct curve_points *this_plot;
  1181.     int             i;        /* point number */
  1182.     double          x, y;
  1183.     double          ylow, yhigh;
  1184.     double          width;
  1185. {
  1186.     struct coordinate GPHUGE *cp = &(this_plot->points[i]);
  1187.  
  1188.     /* the easy part: */
  1189.     cp->type = INRANGE;
  1190.     cp->x = x;
  1191.     cp->y = y;
  1192.     cp->ylow = ylow;
  1193.     cp->yhigh = yhigh;
  1194.     cp->z = width;
  1195.  
  1196.     /* jev -- pass data values thru user-defined function */
  1197.     if (ydata_func.at) {
  1198.     struct value    val;
  1199.  
  1200.     (void) Gcomplex(&ydata_func.dummy_values[0], y, 0.0);
  1201.     evaluate_at(ydata_func.at, &val);
  1202.     cp->y = real(&val);
  1203.  
  1204.     (void) Gcomplex(&ydata_func.dummy_values[0], ylow, 0.0);
  1205.     evaluate_at(ydata_func.at, &val);
  1206.     cp->ylow = real(&val);
  1207.  
  1208.     (void) Gcomplex(&ydata_func.dummy_values[0], yhigh, 0.0);
  1209.     evaluate_at(ydata_func.at, &val);
  1210.     cp->yhigh = real(&val);
  1211.     }
  1212.     /* Adjust for log scale. */
  1213.     if (is_log_x) {
  1214.         cp->type = adjustlog(cp->type, &(cp->x), log_base_log_x);
  1215.         (void) adjustlog(cp->type, &(cp->z), log_base_log_z);
  1216.     }
  1217.     if (is_log_y) {
  1218.     cp->type = adjustlog(cp->type, &(cp->y), log_base_log_y);
  1219.     /* Note ylow,yhigh can't affect cp->type. */
  1220.     (void) adjustlog(cp->type, &(cp->ylow), log_base_log_y);
  1221.     (void) adjustlog(cp->type, &(cp->yhigh), log_base_log_y);
  1222.     }
  1223.     /* Now adjust the xrange, or declare the point out of range */
  1224.     /*
  1225.      * The yrange is handled later, once we know whether to include ylow,
  1226.      * yhigh in the calculation. See adjust_yrange()
  1227.      */
  1228.     if (cp->type == INRANGE)
  1229.     if (autoscale_lx || inrange(x, xmin, xmax)) {
  1230.         if (autoscale_lx) {
  1231.         if (x < xmin)
  1232.             xmin = x;
  1233.         if (x > xmax)
  1234.             xmax = x;
  1235.         }
  1236.     } else {
  1237.         cp->type = OUTRANGE;
  1238.     }
  1239. }
  1240.  
  1241.  
  1242. /*
  1243.  * Adjust for log scale: take the log of the second parameter, in place, if
  1244.  * possible. If not possible, return new type for point; if possible, then
  1245.  * return old type for point.  The log is taken to the base implicit in the
  1246.  * third parameter.
  1247.  */
  1248. static enum coord_type
  1249. adjustlog(type, val, log_base_log)
  1250.     enum coord_type type;
  1251.     coordval       *val;
  1252.     double          log_base_log;
  1253. {
  1254.     if (*val < 0.0) {
  1255.     return (UNDEFINED);
  1256.     } else if (*val == 0.0) {
  1257.     *val = -VERYLARGE;
  1258.     return (OUTRANGE);
  1259.     } else {
  1260.     *val = log(*val)/log_base_log;
  1261.     return (type);
  1262.     }
  1263. }
  1264.  
  1265.  
  1266. /* now adjust the yrange, or declare the point out of range */
  1267. /* this does all points in a curve */
  1268. adjust_yrange(curve)
  1269.     struct curve_points *curve;
  1270. {
  1271.     TBOOLEAN         ebars = (curve->plot_style == ERRORBARS);
  1272.     int             npoints = curve->p_count;    /* number of points */
  1273.     coordval        y, ylow, yhigh;    /* one point value */
  1274.     struct coordinate GPHUGE *cp;    /* one coordinate */
  1275.     int             i;        /* index into points */
  1276.  
  1277.     for (i = 0; i < npoints; i++) {
  1278.     cp = &(curve->points[i]);
  1279.     if (cp->type == INRANGE) {
  1280.         y = is_log_y ? pow(base_log_y, cp->y) : cp->y;
  1281.         if ((autoscale_ly ||
  1282.         /*
  1283.          * inrange((is_log_y ? pow(base_log_y, y) : y), ymin, ymax) ||
  1284.          */
  1285.          inrange((y), ymin, ymax) ||
  1286.          polar)) {
  1287.         if (autoscale_ly) {
  1288.             if (y < ymin)
  1289.             ymin = y;
  1290.             if (y > ymax)
  1291.             ymax = y;
  1292.             if (ebars) {
  1293.             ylow = is_log_y ? pow(base_log_y, cp->ylow) : cp->ylow;
  1294.             yhigh = is_log_y ? pow(base_log_y, cp->yhigh) : cp->yhigh;
  1295.             if (ylow < ymin)
  1296.                 ymin = ylow;
  1297.             if (ylow > ymax)
  1298.                 ymax = ylow;
  1299.             if (yhigh < ymin)
  1300.                 ymin = yhigh;
  1301.             if (yhigh > ymax)
  1302.                 ymax = yhigh;
  1303.             }
  1304.         }
  1305.         } else {
  1306.         cp->type = OUTRANGE;
  1307.         }
  1308.     }
  1309.     }
  1310. }
  1311.  
  1312. grid_nongrid_data(this_plot)
  1313. struct surface_points *this_plot;
  1314. {
  1315.     int i, j, k;
  1316.     double x, y, z, w, dx, dy, xmin, xmax, ymin, ymax;
  1317.     struct iso_curve *old_iso_crvs = this_plot->iso_crvs;
  1318.     struct iso_curve *icrv, *oicrv, *oicrvs;
  1319.  
  1320.     /* Compute XY bounding box on the original data. */
  1321.     xmin = xmax = old_iso_crvs->points[0].x;
  1322.     ymin = ymax = old_iso_crvs->points[0].y;
  1323.     for (icrv = old_iso_crvs; icrv != NULL; icrv = icrv->next) {
  1324.     struct coordinate GPHUGE *points = icrv->points;
  1325.  
  1326.     for (i = 0; i < icrv->p_count; i++, points++) {
  1327.         if (xmin > points->x)
  1328.         xmin = points->x;
  1329.         if (xmax < points->x)
  1330.         xmax = points->x;
  1331.         if (ymin > points->y)
  1332.         ymin = points->y;
  1333.         if (ymax < points->y)
  1334.         ymax = points->y;
  1335.     }
  1336.     }
  1337.  
  1338.     dx = (xmax - xmin) / (dgrid3d_row_fineness - 1);
  1339.     dy = (ymax - ymin) / (dgrid3d_row_fineness - 1);
  1340.  
  1341.     /* Create the new grid structure, and compute the low pass filtering from
  1342.      * non grid to grid structure.
  1343.      */
  1344.     this_plot->iso_crvs = NULL;
  1345.     this_plot->num_iso_read = dgrid3d_col_fineness;
  1346.     this_plot->has_grid_topology = TRUE;
  1347.     for (i = 0, x = xmin; i < dgrid3d_col_fineness; i++, x += dx) {
  1348.     struct coordinate GPHUGE *points;
  1349.  
  1350.     icrv = iso_alloc(dgrid3d_row_fineness + 1);
  1351.     icrv->p_count = dgrid3d_row_fineness;
  1352.     icrv->next = this_plot->iso_crvs;
  1353.     this_plot->iso_crvs = icrv;
  1354.     points = icrv->points;
  1355.  
  1356.     for (j = 0, y = ymin; j < dgrid3d_row_fineness; j++, y += dy, points++) {
  1357.         z = w = 0.0;
  1358.  
  1359.         for (oicrv = old_iso_crvs; oicrv != NULL; oicrv = oicrv->next) {
  1360.         struct coordinate GPHUGE *opoints = oicrv->points;
  1361.         for (k = 0; k < oicrv->p_count; k++, opoints++) {
  1362.             double dist,
  1363.                dist_x = fabs( opoints->x - x ),
  1364.                dist_y = fabs( opoints->y - y );
  1365.  
  1366.             switch (dgrid3d_norm_value) {
  1367.             case 1:
  1368.                 dist = dist_x + dist_y;
  1369.                 break;
  1370.             case 2:
  1371.                 dist = dist_x * dist_x + dist_y * dist_y;
  1372.                 break;
  1373.             case 4:
  1374.                 dist = dist_x * dist_x + dist_y * dist_y;
  1375.                 dist *= dist;
  1376.                 break;
  1377.             case 8:
  1378.                 dist = dist_x * dist_x + dist_y * dist_y;
  1379.                 dist *= dist;
  1380.                 dist *= dist;
  1381.                 break;
  1382.             case 16:
  1383.                 dist = dist_x * dist_x + dist_y * dist_y;
  1384.                 dist *= dist;
  1385.                 dist *= dist;
  1386.                 dist *= dist;
  1387.                 break;
  1388.             default:
  1389.                 dist = pow( dist_x, dgrid3d_norm_value ) +
  1390.                    pow( dist_y, dgrid3d_norm_value );
  1391.                 break;
  1392.             }
  1393.  
  1394.             /* The weight of this point is inverse proportional
  1395.              * to the distance.
  1396.              */
  1397.             if ( dist == 0.0 )
  1398. #ifndef AMIGA_SC_6_1
  1399.             dist = VERYLARGE;
  1400. #else /* AMIGA_SC_6_1 */
  1401.             /* Multiplying VERYLARGE by opoints->z below
  1402.              * might yield Inf (i.e. a number that can't
  1403.              * be represented on the machine). This will
  1404.              * result in points->z being set to NaN. It's
  1405.              * better to have a pretty large number that is
  1406.              * also on the safe side... The numbers that are
  1407.              * read by gnuplot are float values anyway, so
  1408.              * they can't be bigger than FLT_MAX. So setting
  1409.              * dist to FLT_MAX^2 will make dist pretty large
  1410.              * with respect to any value that has been read. */
  1411.             dist = ((double)FLT_MAX)*((double)FLT_MAX);
  1412. #endif /* AMIGA_SC_6_1 */
  1413.             else
  1414.             dist = 1.0 / dist;
  1415.  
  1416.             z += opoints->z * dist;
  1417.             w += dist;
  1418.         }
  1419.         }
  1420.  
  1421.         points->x = x;
  1422.         points->y = y;
  1423.         points->z = z / w;
  1424.         points->type = INRANGE;
  1425.     }
  1426.     }
  1427.     
  1428.     /* Delete the old non grid data. */
  1429.     for (oicrvs = old_iso_crvs; oicrvs != NULL;) {
  1430.     oicrv = oicrvs;
  1431.     oicrvs = oicrvs->next;
  1432.     iso_free(oicrv);
  1433.     }
  1434. }
  1435.  
  1436. get_3ddata(this_plot)
  1437.     struct surface_points *this_plot;
  1438. {
  1439.     register int    i, j, l_num, xdatum, ydatum;
  1440.     float           x, y, z;
  1441.     char            data_file[MAX_LINE_LEN + 1], line[MAX_LINE_LEN + 1];
  1442.     char           *float_format = "%f", *float_skip = "%*f";
  1443.     static TBOOLEAN  only_z = FALSE, using_format = FALSE;
  1444.     static int      xcol = 1, ycol = 2, zcol = 3, index = -1;
  1445.     static char     format[MAX_LINE_LEN + 1];
  1446.     int            pt_in_iso_crv = 0, maxcol, num_col;
  1447.     enum XYZ_order_type {
  1448.     XYZ, YXZ, ZXY, XZY, ZYX, YZX, XY, YX
  1449.     }               xyz_order;
  1450.     struct iso_curve *this_iso;
  1451.  
  1452.     /* close forgotten file (in case of a syntax error) */
  1453.     if (data_fp && !more_data_fp) {
  1454. #if defined(unix) || defined(PIPES)
  1455.     if (pipe_open) {
  1456.         (void) pclose(data_fp);
  1457.         pipe_open = FALSE;
  1458.     } else
  1459. #endif /* unix || PIPES */
  1460.         (void) fclose(data_fp);
  1461.         data_fp=NULL;
  1462.         }
  1463.  
  1464.     quotel_str(data_file, c_token);
  1465.     this_plot->plot_type = DATA3D;
  1466.     this_plot->has_grid_topology = TRUE;
  1467.     if (!more_data_fp) {
  1468. #if defined(unix) || defined(PIPES)
  1469.     if (*data_file == '<') {
  1470.         if ((data_fp = popen(data_file + 1, "r")) == (FILE *) NULL)
  1471.         os_error("cannot create pipe; output not changed", c_token);
  1472.         else
  1473.         pipe_open = TRUE;
  1474.     } else
  1475. #endif /* unix || PIPES */
  1476.         if ((data_fp = fopen(data_file, "r")) == (FILE *) NULL)
  1477.         os_error("can't open data file", c_token);
  1478.  
  1479.     /* Initialize defualt values. */
  1480.     only_z = FALSE;
  1481.     using_format = FALSE;
  1482.     xcol = 1;
  1483.     ycol = 2;
  1484.     zcol = 3;
  1485.     index = -1;
  1486.     format[0] = '\0';
  1487.  
  1488.     c_token++;            /* skip data file name */
  1489.     if (almost_equals(c_token, "i$ndex")) {
  1490.         struct value a;
  1491.         c_token++;        /* skip "index" */
  1492.         index = (int) magnitude(const_express(&a));
  1493.     }
  1494.     if (almost_equals(c_token, "u$sing")) {
  1495.         c_token++;        /* skip "using" */
  1496.         if (!END_OF_COMMAND && !isstring(c_token)) {
  1497.         struct value a;
  1498.         zcol = (int) magnitude(const_express(&a));
  1499.         only_z = TRUE;
  1500.         if (equals(c_token, ":")) {
  1501.             c_token++;    /* skip ":" */
  1502.             only_z = FALSE;
  1503.             ycol = zcol;
  1504.             zcol = (int) magnitude(const_express(&a));
  1505.             if (equals(c_token, ":")) {
  1506.             c_token++;    /* skip ":" */
  1507.             xcol = ycol;
  1508.             ycol = zcol;
  1509.             zcol = (int) magnitude(const_express(&a));
  1510.             } else {
  1511.             if (mapping3d == MAP3D_CARTESIAN)
  1512.                 int_error("Must specify 1 or 3 columns", c_token);
  1513.             xcol = ycol;
  1514.             ycol = zcol;
  1515.             }
  1516.         }
  1517.         if (!only_z)
  1518.             if ((xcol == ycol) || (ycol == zcol) || (xcol == zcol))
  1519.             int_error("Columns must be distinct", c_token);
  1520.         }
  1521.         if (!END_OF_COMMAND && isstring(c_token)) {
  1522.         quotel_str(format, c_token);
  1523.         using_format = TRUE;
  1524.         c_token++;        /* skip format */
  1525.         }
  1526.     } else {
  1527.         if ( (only_z = !parametric) != FALSE)
  1528.         zcol = 1;
  1529.     }
  1530.     }
  1531.  
  1532.     switch (mapping3d) {
  1533.     case MAP3D_CARTESIAN:
  1534.     maxcol = (xcol > ycol) ? xcol : ycol;
  1535.     maxcol = (maxcol > zcol) ? maxcol : zcol;
  1536.     if (!only_z) {        /* Determine ordering of input columns */
  1537.         if (zcol == maxcol) {
  1538.         if (xcol < ycol)
  1539.             xyz_order = XYZ;    /* scanf(x,y,z) */
  1540.         else
  1541.             xyz_order = YXZ;    /* scanf(y,x,z) */
  1542.         } else if (ycol == maxcol) {
  1543.         if (xcol < zcol)
  1544.             xyz_order = XZY;    /* scanf(x,z,y) */
  1545.         else
  1546.             xyz_order = ZXY;    /* scanf(z,x,y) */
  1547.         } else {
  1548.         if (ycol < zcol)
  1549.             xyz_order = YZX;    /* scanf(y,z,x) */
  1550.         else
  1551.             xyz_order = ZYX;    /* scanf(z,y,x) */
  1552.         }
  1553.     }
  1554.     if (strlen(format) == 0) {
  1555.         if (only_z) {
  1556.         for (i = 1; i <= zcol; i++)
  1557.             if (i == zcol)
  1558.             (void) strcat(format, float_format);
  1559.             else
  1560.             (void) strcat(format, float_skip);
  1561.         } else {
  1562.         for (i = 1; i <= maxcol; i++)
  1563.             if ((i == xcol) || (i == ycol) || (i == zcol))
  1564.             (void) strcat(format, float_format);
  1565.             else
  1566.             (void) strcat(format, float_skip);
  1567.         }
  1568.     }
  1569.     break;
  1570.     case MAP3D_SPHERICAL:
  1571.     case MAP3D_CYLINDRICAL:
  1572.     if (only_z)
  1573.         int_error("Two or three columns for spherical/cylindrical coords.", c_token);
  1574.     maxcol = (xcol > ycol) ? xcol : ycol;
  1575.     maxcol = (maxcol > zcol) ? maxcol : zcol;
  1576.     xyz_order = (xcol < ycol) ? XY : YX;
  1577.     for (i = 1; i <= maxcol; i++)
  1578.         if ((i == xcol) || (i == ycol))
  1579.         (void) strcat(format, float_format);
  1580.         else
  1581.         (void) strcat(format, float_skip);
  1582.     }
  1583.  
  1584.     l_num = 0;
  1585.     xdatum = 0;
  1586.     ydatum = 0;
  1587.     this_plot->num_iso_read = 0;
  1588.     this_plot->has_grid_topology = TRUE;
  1589.     if (this_plot->iso_crvs != NULL) {
  1590.     struct iso_curve *icrv, *icrvs = this_plot->iso_crvs;
  1591.  
  1592.     while (icrvs) {
  1593.         icrv = icrvs;
  1594.         icrvs = icrvs->next;
  1595.         iso_free(icrv);
  1596.     }
  1597.     this_plot->iso_crvs = NULL;
  1598.     }
  1599.     if (!more_data_fp && is_binary_file(data_fp)) {    /* MOD--RKC */
  1600. #if defined(MSDOS)||defined(ATARI)||defined(OS2)||defined(_Windows)||defined(DOS386)
  1601.     /* file must be opened with binary flag. the old cr/lf problem again */
  1602. #ifdef PIPES
  1603.     if( pipe_open ) {
  1604.       pclose(data_fp);
  1605.       data_fp=NULL;
  1606.       pipe_open=FALSE;
  1607.       int_error("binary data from pipes is not implemented", NO_CARET);
  1608.     }
  1609. #endif
  1610.     data_fp = freopen(data_file, "rb", data_fp);
  1611. #endif
  1612.     xdatum = get_binary_data(this_plot, data_fp, &this_iso);
  1613.     } else {
  1614.     int last_line_blank = FALSE;
  1615.  
  1616.     more_data_fp = FALSE;
  1617.  
  1618.     this_iso = iso_alloc(samples);
  1619.  
  1620.     if (index > 0) { /* Skip data meshes until mesh index is reached. */
  1621.         int i = index;
  1622.  
  1623.         while (i--) {
  1624.         while (fgets(line, MAX_LINE_LEN, data_fp) != (char *) NULL) {
  1625.             l_num++;
  1626.  
  1627.             if (!line[1]) {    /* is it blank line ? */
  1628.             if (last_line_blank) /* Two consecutive blanks. */
  1629.                 break;
  1630.             last_line_blank = TRUE;
  1631.             }
  1632.             else
  1633.             last_line_blank = FALSE;
  1634.         }
  1635.         if (feof(data_fp))
  1636.             int_error("mesh index overflow", NO_CARET);
  1637.         }
  1638.     }   /* end of index skip */
  1639.     
  1640.     last_line_blank = FALSE;
  1641.  
  1642.     while (fgets(line, MAX_LINE_LEN, data_fp) != (char *) NULL) {
  1643.         l_num++;
  1644.         if (is_comment(line[0]))
  1645.         continue;    /* ignore comments */
  1646.         if (!line[1]) {    /* is it blank line ? */
  1647.         if (last_line_blank) { /* Two consecutive blank lines. */
  1648.             more_data_fp = TRUE;
  1649.             break;
  1650.         }
  1651.         last_line_blank = TRUE;
  1652.  
  1653.         if (pt_in_iso_crv == 0) {
  1654.             if (xdatum == 0)
  1655.             continue;
  1656.             pt_in_iso_crv = xdatum;
  1657.         }
  1658.         if (xdatum > 0) {
  1659.             this_iso->p_count = xdatum;
  1660.             this_iso->next = this_plot->iso_crvs;
  1661.             this_plot->iso_crvs = this_iso;
  1662.             this_plot->num_iso_read++;
  1663.  
  1664.             if (xdatum != pt_in_iso_crv)
  1665.             this_plot->has_grid_topology = FALSE;
  1666.  
  1667.             this_iso = iso_alloc(pt_in_iso_crv);
  1668.             xdatum = 0;
  1669.             ydatum++;
  1670.         }
  1671.         continue;
  1672.         }
  1673.         last_line_blank = FALSE;
  1674.         if (xdatum >= this_iso->p_max) {
  1675.         /*
  1676.          * overflow about to occur. Extend size of points[] array. We
  1677.          * either double the size, or add 1000 points, whichever is a
  1678.          * smaller increment. Note i=p_max.
  1679.          */
  1680.         iso_extend(this_iso,
  1681.                xdatum + (xdatum < 1000 ? xdatum : 1000));
  1682.         }
  1683.         switch (num_col = sscanf(line, format, &x, &y, &z)) {
  1684.         case 3:        /* All parameter are specified. */
  1685.         if (!only_z || using_format) {
  1686.             switch (xyz_order) {
  1687.             case XYZ:    /* scanf(x,y,z) */
  1688.             this_iso->points[xdatum].x = x;
  1689.             this_iso->points[xdatum].y = y;
  1690.             this_iso->points[xdatum].z = z;
  1691.             break;
  1692.             case XZY:    /* scanf(x,z,y) */
  1693.             this_iso->points[xdatum].x = x;
  1694.             this_iso->points[xdatum].y = z;
  1695.             this_iso->points[xdatum].z = y;
  1696.             break;
  1697.             case YXZ:    /* scanf(y,x,z) */
  1698.             this_iso->points[xdatum].x = y;
  1699.             this_iso->points[xdatum].y = x;
  1700.             this_iso->points[xdatum].z = z;
  1701.             break;
  1702.             case ZXY:    /* scanf(z,x,y) */
  1703.             this_iso->points[xdatum].x = y;
  1704.             this_iso->points[xdatum].y = z;
  1705.             this_iso->points[xdatum].z = x;
  1706.             break;
  1707.             case YZX:    /* scanf(y,z,x) */
  1708.             this_iso->points[xdatum].x = z;
  1709.             this_iso->points[xdatum].y = x;
  1710.             this_iso->points[xdatum].z = y;
  1711.             break;
  1712.             case ZYX:    /* scanf(z,y,x) */
  1713.             this_iso->points[xdatum].x = z;
  1714.             this_iso->points[xdatum].y = y;
  1715.             this_iso->points[xdatum].z = x;
  1716.             break;
  1717.             }
  1718.             if (xyz_order != XYZ) {
  1719.             x = this_iso->points[xdatum].x;
  1720.             y = this_iso->points[xdatum].y;
  1721.             z = this_iso->points[xdatum].z;
  1722.             }
  1723.             if (!parametric)
  1724.             int_error("Must be in parametric mode.",
  1725.                   NO_CARET);
  1726.             break;
  1727.         }
  1728.         case 1:        /* only one number on the line */
  1729.         if (!only_z && !using_format)
  1730.             int_error("3 columns expected, only 1 found", c_token);
  1731.         /* assign that number to z */
  1732.         this_iso->points[xdatum].z = x;
  1733.         z = x;
  1734.         this_iso->points[xdatum].x = xdatum;
  1735.         x = this_iso->points[xdatum].x;
  1736.         this_iso->points[xdatum].y = ydatum;
  1737.         y = this_iso->points[xdatum].y;
  1738.         if (parametric)
  1739.             int_error("Must be in non parametric mode.",
  1740.                   NO_CARET);
  1741.         break;
  1742.         case 2:
  1743.         switch (xyz_order) {
  1744.         case YX:
  1745.             z = x;    /* Use z as temp */
  1746.             x = y;
  1747.             y = z;
  1748.             break;
  1749.         default:
  1750.             break;
  1751.         }
  1752.         switch (mapping3d) {
  1753.         case MAP3D_CARTESIAN:
  1754.             int_error("2 columns found, 1 or 3 expected",
  1755.                   c_token);
  1756.             break;
  1757.         case MAP3D_SPHERICAL:
  1758.             if (angles_format == ANGLES_DEGREES) {
  1759.             x *= DEG2RAD;    /* Convert to radians. */
  1760.             y *= DEG2RAD;
  1761.             }
  1762.             if( num_col == 2) z = 1.0;
  1763.             this_iso->points[xdatum].x = z*cos(x) * cos(y);
  1764.             this_iso->points[xdatum].y = z*sin(x) * cos(y);
  1765.             this_iso->points[xdatum].z = z*sin(y);
  1766.             break;
  1767.         case MAP3D_CYLINDRICAL:
  1768.             if (angles_format == ANGLES_DEGREES)
  1769.             x *= DEG2RAD;    /* Convert to radians. */
  1770.             if( num_col == 2) z = 1.0;
  1771.             this_iso->points[xdatum].x = z*cos(x);
  1772.             this_iso->points[xdatum].y = z*sin(x);
  1773.             this_iso->points[xdatum].z = y;
  1774.             break;
  1775.         }
  1776.         x = this_iso->points[xdatum].x;
  1777.         y = this_iso->points[xdatum].y;
  1778.         z = this_iso->points[xdatum].z;
  1779.         break;
  1780.         default:
  1781.         (void) sprintf(line, "bad data on line %d", l_num);
  1782.         int_error(line, c_token);
  1783.         }
  1784.  
  1785.         if (is_log_x) {
  1786.         if (x <= 0.0)
  1787.             int_error("X value must be above 0 for log scale!",
  1788.                   NO_CARET);
  1789.         else
  1790.             this_iso->points[xdatum].x =
  1791.             log(this_iso->points[xdatum].x)/log_base_log_x;
  1792.         }
  1793.         if (is_log_y) {
  1794.         if (y <= 0.0)
  1795.             int_error("Y value must be above 0 for log scale!",
  1796.                   NO_CARET);
  1797.         else
  1798.             this_iso->points[xdatum].y =
  1799.             log(this_iso->points[xdatum].y)/log_base_log_y;
  1800.         }
  1801.         if (is_log_z) {
  1802.         if (z <= 0.0)
  1803.             int_error("Z value must be above 0 for log scale!",
  1804.                   NO_CARET);
  1805.         else
  1806.             this_iso->points[xdatum].z =
  1807.             log(this_iso->points[xdatum].z)/log_base_log_z;
  1808.         }
  1809.         if (autoscale_lx) {
  1810.         if (x < xmin)
  1811.             xmin = x;
  1812.         if (x > xmax)
  1813.             xmax = x;
  1814.         }
  1815.         if (autoscale_ly) {
  1816.         if (y < ymin)
  1817.             ymin = y;
  1818.         if (y > ymax)
  1819.             ymax = y;
  1820.         }
  1821.         if (autoscale_lz) {
  1822.         if (z < zmin)
  1823.             zmin = z;
  1824.         if (z > zmax)
  1825.             zmax = z;
  1826.         }
  1827.         xdatum++;    
  1828.     }  /* end of while loop */
  1829.  
  1830.     if (xdatum > 0) {
  1831.         this_plot->num_iso_read++;    /* Update last iso. */
  1832.         this_iso->p_count = xdatum;
  1833.  
  1834.         this_iso->next = this_plot->iso_crvs;
  1835.         this_plot->iso_crvs = this_iso;
  1836.  
  1837.         if (xdatum != pt_in_iso_crv)
  1838.         this_plot->has_grid_topology = FALSE;
  1839.  
  1840.     } else {
  1841.         iso_free(this_iso);    /* Free last allocation. */
  1842.     }
  1843.     }                /* MOD-RKC else of binary */
  1844.  
  1845.     if (index >= 0) more_data_fp = FALSE; /* Only one data set please. */
  1846.  
  1847.     if (!more_data_fp) {
  1848.       if (this_plot->num_iso_read <= 1)
  1849.       this_plot->has_grid_topology = FALSE;
  1850. #if defined(unix) || defined(PIPES)
  1851.       if (pipe_open) {
  1852.     if (this_plot->has_grid_topology && !hidden3d) {
  1853.       (void) pclose(data_fp);
  1854.       pipe_open = FALSE;
  1855.     }
  1856.       } else 
  1857. #endif /* unix || PIPES */
  1858.       {
  1859.     (void) fclose(data_fp);  
  1860.     data_fp = NULL;
  1861.       }
  1862.     }
  1863.  
  1864.     if (dgrid3d) grid_nongrid_data(this_plot);
  1865.  
  1866.     if (this_plot->num_iso_read <= 1)
  1867.     this_plot->has_grid_topology = FALSE;
  1868.     if (this_plot->has_grid_topology && !hidden3d) {
  1869.     struct iso_curve *new_icrvs = NULL;
  1870.     int             num_new_iso = this_plot->iso_crvs->p_count, len_new_iso = this_plot->num_iso_read;
  1871.  
  1872.     /* Now we need to set the other direction (pseudo) isolines. */
  1873.     for (i = 0; i < num_new_iso; i++) {
  1874.         struct iso_curve *new_icrv = iso_alloc(len_new_iso);
  1875.  
  1876.         new_icrv->p_count = len_new_iso;
  1877.  
  1878.         for (j = 0, this_iso = this_plot->iso_crvs;
  1879.          this_iso != NULL;
  1880.          j++, this_iso = this_iso->next) {
  1881.         new_icrv->points[j].x = this_iso->points[i].x;
  1882.         new_icrv->points[j].y = this_iso->points[i].y;
  1883.         new_icrv->points[j].z = this_iso->points[i].z;
  1884.         }
  1885.  
  1886.         new_icrv->next = new_icrvs;
  1887.         new_icrvs = new_icrv;
  1888.     }
  1889.  
  1890.     /* Append the new iso curves after the read ones. */
  1891.     for (this_iso = this_plot->iso_crvs;
  1892.          this_iso->next != NULL;
  1893.          this_iso = this_iso->next);
  1894.         this_iso->next = new_icrvs;
  1895.     }
  1896. }
  1897.  
  1898. /*
  1899.  * print_points: a debugging routine to print out the points of a curve, and
  1900.  * the curve structure. If curve<0, then we print the list of curves.
  1901.  */
  1902.  
  1903. static char    *plot_type_names[4] =
  1904. {
  1905.     "Function", "Data", "3D Function", "3d data"
  1906. };
  1907. static char    *plot_style_names[6] =
  1908. {
  1909.     "Lines", "Points", "Impulses", "LinesPoints", "Dots", "Errorbars"
  1910. };
  1911.  
  1912. print_points(curve)
  1913.     int             curve;    /* which curve to print */
  1914. {
  1915.     register struct curve_points *this_plot;
  1916.     int             i;
  1917.  
  1918.     if (curve < 0) {
  1919.     for (this_plot = first_plot, i = 0;
  1920.          this_plot != NULL;
  1921.          i++, this_plot = this_plot->next_cp) {
  1922.         printf("Curve %d:\n", i);
  1923.         if ((int) this_plot->plot_type >= 0 && (int) (this_plot->plot_type) < 4)
  1924.         printf("Plot type %d: %s\n", (int) (this_plot->plot_type),
  1925.                plot_type_names[(int) (this_plot->plot_type)]);
  1926.         else
  1927.         printf("Plot type %d: BAD\n", (int) (this_plot->plot_type));
  1928.         if ((int) this_plot->plot_style >= 0 && (int) (this_plot->plot_style) < 6)
  1929.         printf("Plot style %d: %s\n", (int) (this_plot->plot_style),
  1930.                plot_style_names[(int) (this_plot->plot_style)]);
  1931.         else
  1932.         printf("Plot style %d: BAD\n", (int) (this_plot->plot_style));
  1933.         printf("Plot title: '%s'\n", this_plot->title);
  1934.         printf("Line type %d\n", this_plot->line_type);
  1935.         printf("Point type %d\n", this_plot->point_type);
  1936.         printf("max points %d\n", this_plot->p_max);
  1937.         printf("current points %d\n", this_plot->p_count);
  1938.         printf("\n");
  1939.     }
  1940.     } else {
  1941.     for (this_plot = first_plot, i = 0;
  1942.          i < curve && this_plot != NULL;
  1943.          i++, this_plot = this_plot->next_cp);
  1944.     if (this_plot == NULL)
  1945.         printf("Curve %d does not exist; list has %d curves\n", curve, i);
  1946.     else {
  1947.         printf("Curve %d, %d points\n", curve, this_plot->p_count);
  1948.         for (i = 0; i < this_plot->p_count; i++) {
  1949.         printf("%c x=%g y=%g z=%g ylow=%g yhigh=%g\n",
  1950.                this_plot->points[i].type == INRANGE ? 'i'
  1951.                : this_plot->points[i].type == OUTRANGE ? 'o'
  1952.                : 'u',
  1953.                this_plot->points[i].x,
  1954.                this_plot->points[i].y,
  1955.                this_plot->points[i].z,
  1956.                this_plot->points[i].ylow,
  1957.                this_plot->points[i].yhigh);
  1958.         }
  1959.         printf("\n");
  1960.     }
  1961.     }
  1962. }
  1963.  
  1964. print_table()
  1965. {
  1966.     register struct curve_points *this_plot;
  1967.     int             i, curve;
  1968.  
  1969.     for (this_plot = first_plot, curve = 0; this_plot != NULL;
  1970.      curve++, this_plot = this_plot->next_cp) {
  1971.     fprintf(outfile, "Curve %d, %d points\n", curve, this_plot->p_count);
  1972.     for (i = 0; i < this_plot->p_count; i++) {
  1973.         fprintf(outfile, "%c x=%g y=%g\n",
  1974.             this_plot->points[i].type == INRANGE ? 'i'
  1975.             : this_plot->points[i].type == OUTRANGE ? 'o'
  1976.             : 'u',
  1977.             this_plot->points[i].x,
  1978.             this_plot->points[i].y);
  1979.     }
  1980.     fprintf(outfile, "\n");
  1981.     }
  1982.     fflush(outfile);
  1983. }
  1984.  
  1985. print_3dtable(pcount)
  1986. int pcount;
  1987. {
  1988.     register struct surface_points *this_plot;
  1989.     int             i, curve,surface;
  1990.     struct iso_curve *icrvs;
  1991.     struct coordinate GPHUGE *points;
  1992.  
  1993.     for (surface=0, this_plot=first_3dplot ; surface < pcount; 
  1994.         this_plot=this_plot->next_sp, surface++){
  1995.         fprintf(outfile, "\nSurface %d of %d surfaces\n", surface, pcount);
  1996.         icrvs = this_plot->iso_crvs;
  1997.         curve = 0;
  1998.  
  1999.         while(icrvs){
  2000.             fprintf(outfile, "\nIsoCurve %d, %d points\n", curve, icrvs->p_count);
  2001.             for(i=0, points = icrvs->points; i < icrvs->p_count; i++){
  2002.                 fprintf(outfile, "%c x=%g y=%g z=%g\n",
  2003.                 points[i].type == INRANGE ? 'i'
  2004.                 : points[i].type == OUTRANGE ? 'o'
  2005.                 : 'u',
  2006.                 points[i].x,
  2007.                 points[i].y,
  2008.                 points[i].z);
  2009.             }
  2010.             icrvs = icrvs->next;
  2011.             curve++;
  2012.         }
  2013.     fprintf(outfile, "\n");
  2014.     }
  2015.     fflush(outfile);
  2016. }
  2017.  
  2018. /*
  2019.  * This parses the plot command after any range specifications. To support
  2020.  * autoscaling on the x axis, we want any data files to define the x range,
  2021.  * then to plot any functions using that range. We thus parse the input
  2022.  * twice, once to pick up the data files, and again to pick up the functions.
  2023.  * Definitions are processed twice, but that won't hurt.
  2024.  */
  2025. eval_plots()
  2026. {
  2027.     register int    i;
  2028.     register struct curve_points *this_plot, **tp_ptr;
  2029.     register int    start_token, end_token;
  2030.     register int    begin_token;
  2031.     double          x_min, x_max, y_min, y_max;
  2032.     register double x, xdiff, temp;
  2033.     static struct value a;
  2034.     TBOOLEAN         ltmp, some_data_files = FALSE,is_log_func = FALSE;
  2035.     int             plot_num, line_num, point_num, xparam = 0;
  2036.     char           *xtitle;
  2037.     void            parametric_fixup();
  2038.  
  2039.     /* Reset first_plot. This is usually done at the end of this function.
  2040.        If there is an error within this function, the memory is left allocated,
  2041.        since we cannot call cp_free if the list is incomplete. Making sure that
  2042.        the list structure is always vaild requires some rewriting */
  2043.     first_plot=NULL;
  2044.  
  2045.     if (autoscale_ly) {
  2046.     ymin = VERYLARGE;
  2047.     ymax = -VERYLARGE;
  2048.     } else if (is_log_y && (ymin <= 0.0 || ymax <= 0.0))
  2049.     int_error("y range must be above 0 for log scale!",
  2050.           NO_CARET);
  2051.  
  2052.     tp_ptr = &(first_plot);
  2053.     plot_num = 0;
  2054.     line_num = 0;        /* default line type */
  2055.     point_num = 0;        /* default point type */
  2056.  
  2057.     xtitle = NULL;
  2058.  
  2059.     begin_token = c_token;
  2060.  
  2061.     /*** First Pass: Read through data files ***
  2062.      * This pass serves to set the xrange and to parse the command, as well
  2063.      * as filling in every thing except the function data. That is done after
  2064.      * the xrange is defined.
  2065.      */
  2066.     while (TRUE) {
  2067.     if (END_OF_COMMAND)
  2068.         int_error("function to plot expected", c_token);
  2069.  
  2070.     start_token = c_token;
  2071.  
  2072.     if (is_definition(c_token)) {
  2073.         define();
  2074.     } else {
  2075.         plot_num++;
  2076.  
  2077.         if (isstring(c_token)) {    /* data file to plot */
  2078.         if (parametric && xparam)
  2079.             int_error("previous parametric function not fully specified",
  2080.                   c_token);
  2081.  
  2082.         if (!some_data_files && autoscale_lx) {
  2083.             xmin = VERYLARGE;
  2084.             xmax = -VERYLARGE;
  2085.         }
  2086.         some_data_files = TRUE;
  2087.  
  2088.         if (*tp_ptr)
  2089.             this_plot = *tp_ptr;
  2090.         else {        /* no memory malloc()'d there yet */
  2091.             this_plot = cp_alloc(MIN_CRV_POINTS);
  2092.             *tp_ptr = this_plot;
  2093.         }
  2094.         this_plot->plot_type = DATA;
  2095.         this_plot->plot_style = data_style;
  2096.         end_token = c_token;
  2097.         get_data(this_plot);    /* this also parses the using option */
  2098.         } else {        /* function to plot */
  2099.         if (parametric)    /* working on x parametric function */
  2100.             xparam = 1 - xparam;
  2101.         if (*tp_ptr) {
  2102.             this_plot = *tp_ptr;
  2103.             cp_extend(this_plot, samples + 1);
  2104.         } else {    /* no memory malloc()'d there yet */
  2105.             this_plot = cp_alloc(samples + 1);
  2106.             *tp_ptr = this_plot;
  2107.         }
  2108.         this_plot->plot_type = FUNC;
  2109.         this_plot->plot_style = func_style;
  2110.         dummy_func = &plot_func;
  2111.         plot_func.at = temp_at();
  2112.         /* ignore it for now */
  2113.         end_token = c_token - 1;
  2114.         }
  2115.  
  2116.         if (almost_equals(c_token, "t$itle")) {
  2117.         if (parametric) {
  2118.             if (xparam)
  2119.             int_error(
  2120.                      "\"title\" allowed only after parametric function fully specified",
  2121.                      c_token);
  2122.             else if (xtitle != NULL)
  2123.             xtitle[0] = '\0';    /* Remove default title . */
  2124.         }
  2125.         c_token++;
  2126.         if (isstring(c_token)) {
  2127.             m_quote_capture(&(this_plot->title), c_token, c_token);
  2128.         } else {
  2129.             int_error("expecting \"title\" for plot", c_token);
  2130.         }
  2131.         c_token++;
  2132.         } else if (almost_equals(c_token, "not$itle")) {
  2133.             c_token++;
  2134.         } else {
  2135.         m_capture(&(this_plot->title), start_token, end_token);
  2136.         if (xparam)
  2137.             xtitle = this_plot->title;
  2138.         }
  2139.  
  2140.         this_plot->line_type = line_num;
  2141.         this_plot->point_type = point_num;
  2142.  
  2143.         if (almost_equals(c_token, "w$ith")) {
  2144.         if (parametric && xparam)
  2145.             int_error("\"with\" allowed only after parametric function fully specified",
  2146.                   c_token);
  2147.         this_plot->plot_style = get_style();
  2148.         }
  2149.         if (!equals(c_token, ",") && !END_OF_COMMAND) {
  2150.         struct value    t;
  2151.         this_plot->line_type = (int) real(const_express(&t)) - 1;
  2152.         }
  2153.         if (!equals(c_token, ",") && !END_OF_COMMAND) {
  2154.         struct value    t;
  2155.         this_plot->point_type = (int) real(const_express(&t)) - 1;
  2156.         }
  2157.         if ((this_plot->plot_style == POINTSTYLE) ||
  2158.         (this_plot->plot_style == LINESPOINTS) ||
  2159.         (this_plot->plot_style == ERRORBARS))
  2160.         if (!xparam)
  2161.             point_num++;
  2162.         if (!xparam)
  2163.         line_num++;
  2164.  
  2165.         if (this_plot->plot_type == DATA)
  2166.         /* now that we know the plot style, adjust the yrange */
  2167.         adjust_yrange(this_plot);
  2168.  
  2169.         tp_ptr = &(this_plot->next_cp);
  2170.     }
  2171.  
  2172.     if (equals(c_token, ","))
  2173.         c_token++;
  2174.     else
  2175.         break;
  2176.     }
  2177.  
  2178.     if (parametric && xparam)
  2179.     int_error("parametric function not fully specified", NO_CARET);
  2180.  
  2181.     if (parametric) {
  2182.     /* Swap t and x ranges for duration of these eval_plot computations. */
  2183.     ltmp = autoscale_lx;
  2184.     autoscale_lx = autoscale_lt;
  2185.     autoscale_lt = ltmp;
  2186.     temp = xmin;
  2187.     xmin = tmin;
  2188.     tmin = temp;
  2189.     temp = xmax;
  2190.     xmax = tmax;
  2191.     tmax = temp;
  2192.     }
  2193.     /*** Second Pass: Evaluate the functions ***/
  2194.     /*
  2195.      * Everything is defined now, except the function data. We expect no
  2196.      * syntax errors, etc, since the above parsed it all. This makes the code
  2197.      * below simpler. If autoscale_ly, the yrange may still change.
  2198.      */
  2199.     if (fabs(xmax - xmin) < zero)
  2200.     if (autoscale_lx) {
  2201.         fprintf(stderr, "Warning: empty %c range [%g:%g], ",
  2202.             parametric ? 't' : 'x', xmin, xmax);
  2203.         if (fabs(xmin) < zero) {
  2204.         /* completely arbitary */
  2205.         xmin = -1.;
  2206.         xmax = 1.;
  2207.         } else {
  2208.         /* expand range by 10% in either direction */
  2209.         xmin = xmin * 0.9;
  2210.         xmax = xmax * 1.1;
  2211.         }
  2212.         fprintf(stderr, "adjusting to [%g:%g]\n", xmin, xmax);
  2213.     } else {
  2214.         int_error("x range is less than `zero`", c_token);
  2215.     }
  2216.  
  2217.     /* give error if xrange badly set from missing datafile error */
  2218.     if (xmin == VERYLARGE || xmax == -VERYLARGE) {
  2219.     int_error("x range is invalid", c_token);
  2220.     }
  2221.     if (is_log_x) {
  2222.     if (xmin <= 0.0 || xmax <= 0.0)
  2223.         int_error("x range must be greater than 0 for log scale!", NO_CARET);
  2224.     x_min = log(xmin)/log_base_log_x;
  2225.     x_max = log(xmax)/log_base_log_x;
  2226.     } else {
  2227.     x_min = xmin;
  2228.     x_max = xmax;
  2229.     }
  2230.  
  2231.     xdiff = (x_max - x_min) / (samples - 1);
  2232.  
  2233.     tp_ptr = &(first_plot);
  2234.     plot_num = 0;
  2235.     this_plot = first_plot;
  2236.     c_token = begin_token;    /* start over */
  2237.  
  2238.     /* Read through functions */
  2239.     while (TRUE) {
  2240.     if (is_definition(c_token)) {
  2241.         define();
  2242.     } else {
  2243.         plot_num++;
  2244.         if (isstring(c_token)) {    /* data file to plot */
  2245.         /* ignore this now */
  2246.         c_token++;
  2247.  
  2248.         /*
  2249.          * jev -- support for passing data from file thru user
  2250.          * function
  2251.          */
  2252.         if (almost_equals(c_token, "thru$")) {
  2253.             struct udft_entry tmp;
  2254.             c_token++;
  2255.             dummy_func = &tmp;
  2256.             (void) temp_at();
  2257.         }
  2258.         if (almost_equals(c_token, "u$sing")) {
  2259.             c_token++;    /* skip "using" */
  2260.             if (!isstring(c_token)) {
  2261.             struct value    a;
  2262.             (void) magnitude(const_express(&a));    /* skip xcol */
  2263.             if (equals(c_token, ":")) {
  2264.                 c_token++;    /* skip ":" */
  2265.                 (void) magnitude(const_express(&a));    /* skip ycol */
  2266.             }
  2267.             if (equals(c_token, ":")) {
  2268.                 c_token++;    /* skip ":" */
  2269.                 (void) magnitude(const_express(&a));    /* skip yemin */
  2270.             }
  2271.             if (equals(c_token, ":")) {
  2272.                 c_token++;    /* skip ":" */
  2273.                 (void) magnitude(const_express(&a));    /* skip yemax */
  2274.             }
  2275.             if (equals(c_token, ":")) {
  2276.                 c_token++;    /* skip ":" */
  2277.                 (void) magnitude(const_express(&a));    /* skip wcol */
  2278.             }
  2279.             }
  2280.             if (isstring(c_token))
  2281.             c_token++;    /* skip format string */
  2282.         }
  2283.         } else {        /* function to plot */
  2284.         if (parametric)    /* working on x parametric function */
  2285.             xparam = 1 - xparam;
  2286.         dummy_func = &plot_func;
  2287.         plot_func.at = temp_at();    /* reparse function */
  2288.  
  2289.         is_log_func=parametric?(xparam?is_log_x:is_log_y):is_log_y;
  2290.         for (i = 0; i < samples; i++) {
  2291.             x = x_min + i * xdiff;
  2292.             /* if (is_log_x) PEM fix logscale x axis */
  2293.             /* x = pow(base_log_x,x); 26-Sep-89 */
  2294.             (void) Gcomplex(&plot_func.dummy_values[0],
  2295.                    is_log_x ? pow(base_log_x, x) : x,
  2296.                    0.0);
  2297.  
  2298.             evaluate_at(plot_func.at, &a);
  2299.  
  2300.             if (undefined || (fabs(imag(&a)) > zero)) {
  2301.             this_plot->points[i].type = UNDEFINED;
  2302.             continue;
  2303.             }
  2304.             temp = real(&a);
  2305.             
  2306.             if (is_log_func && temp < 0.0) {
  2307.             this_plot->points[i].type = UNDEFINED;
  2308.             continue;
  2309.             }
  2310.             this_plot->points[i].x = x;
  2311.             this_plot->points[i].z = -1.0;  /* width of box not specified */
  2312.  
  2313.             if (is_log_func) {
  2314.             if (temp == 0.0) {
  2315.                 this_plot->points[i].type = OUTRANGE;
  2316.                 this_plot->points[i].y = -VERYLARGE;
  2317.                 continue;
  2318.             } else {
  2319.                 this_plot->points[i].y = log(temp)/log_base_log_y;
  2320.             }
  2321.             } else
  2322.             this_plot->points[i].y = temp;
  2323.  
  2324.             if (autoscale_ly || polar
  2325.             || inrange(temp, ymin, ymax)) {
  2326.             this_plot->points[i].type = INRANGE;
  2327.             /* When xparam is 1 we are not really computing y's! */
  2328.             if (!xparam && autoscale_ly) {
  2329.                 if (temp < ymin)
  2330.                 ymin = temp;
  2331.                 if (temp > ymax)
  2332.                 ymax = temp;
  2333.             }
  2334.             } else
  2335.             this_plot->points[i].type = OUTRANGE;
  2336.         }
  2337.         this_plot->p_count = i;    /* samples */
  2338.         }
  2339.  
  2340.         /* title was handled above */
  2341.         if (almost_equals(c_token, "t$itle")) {
  2342.         c_token++;
  2343.         c_token++;
  2344.         } else if (almost_equals(c_token, "not$itle")) {
  2345.             c_token++;
  2346.         }
  2347.         /* style was handled above */
  2348.         if (almost_equals(c_token, "w$ith")) {
  2349.         c_token++;
  2350.         c_token++;
  2351.         }
  2352.         /* line and point types were handled above */
  2353.         if (!equals(c_token, ",") && !END_OF_COMMAND) {
  2354.         struct value    t;
  2355.         (void) real(const_express(&t));
  2356.         }
  2357.         if (!equals(c_token, ",") && !END_OF_COMMAND) {
  2358.         struct value    t;
  2359.         (void) real(const_express(&t));
  2360.         }
  2361.         tp_ptr = &(this_plot->next_cp);    /* used below */
  2362.         this_plot = this_plot->next_cp;
  2363.     }
  2364.  
  2365.     if (equals(c_token, ","))
  2366.         c_token++;
  2367.     else
  2368.         break;
  2369.     }
  2370.  
  2371.     /* throw out all curve_points at end of list, that we don't need  */
  2372.     cp_free(*tp_ptr);
  2373.     *tp_ptr = NULL;
  2374.  
  2375.     /* if first_plot is NULL, we have no functions or data at all. This can
  2376.        happen, if you type "plot x=5", since x=5 is a variable assignment */
  2377.  
  2378.     if(first_plot==NULL) {
  2379.     int_error("no functions or data to plot", c_token);
  2380.     }
  2381.  
  2382.     if (fabs(ymax - ymin) < zero)
  2383.     /* if autoscale, widen range */
  2384.     if (autoscale_ly) {
  2385.         fprintf(stderr, "Warning: empty y range [%g:%g], ", ymin, ymax);
  2386.         if (fabs(ymin) < zero) {
  2387.         ymin = -1.;
  2388.         ymax = 1.;
  2389.         } else {
  2390.         /* expand range by 10% in either direction */
  2391.         ymin = ymin * 0.9;
  2392.         ymax = ymax * 1.1;
  2393.         }
  2394.         fprintf(stderr, "adjusting to [%g:%g]\n", ymin, ymax);
  2395.     } else {
  2396.         int_error("y range is less than `zero`", c_token);
  2397.     }
  2398.  
  2399.     /* Now we finally know the real ymin and ymax */
  2400.     if (is_log_y) {
  2401.     y_min = log(ymin)/log_base_log_y;
  2402.     y_max = log(ymax)/log_base_log_y;
  2403.     } else {
  2404.     y_min = ymin;
  2405.     y_max = ymax;
  2406.     }
  2407.  
  2408.     /* Set a flag so capture is not invoked  by replot itself. -hmh */
  2409.     if (plot_token != -1) {
  2410.         capture(replot_line, plot_token, c_token);
  2411.         plot_token = -1;        
  2412.     }
  2413.   
  2414.     if (parametric) {
  2415.     /* Now put t and x ranges back before we actually plot anything. */
  2416.     ltmp = autoscale_lx;
  2417.     autoscale_lx = autoscale_lt;
  2418.     autoscale_lt = ltmp;
  2419.     temp = xmin;
  2420.     xmin = tmin;
  2421.     tmin = temp;
  2422.     temp = xmax;
  2423.     xmax = tmax;
  2424.     tmax = temp;
  2425.     if (some_data_files && autoscale_lx) {
  2426.         /*
  2427.          * Stop any further autoscaling in this case (may be a mistake,
  2428.          * have to consider what is really wanted some day in the
  2429.          * future--jdc).
  2430.          */
  2431.         autoscale_lx = 0;
  2432.     }
  2433.     /* Now actually fix the plot pairs to be single plots. */
  2434.     parametric_fixup(first_plot, &plot_num, &x_min, &x_max);
  2435.     }
  2436.     if (strcmp(term_tbl[term].name, "table") == 0)
  2437.     print_table();
  2438.     else
  2439.     do_plot(first_plot, plot_num, x_min, x_max, y_min, y_max);
  2440.     cp_free(first_plot);
  2441.     first_plot = NULL;
  2442. }
  2443.  
  2444. static void 
  2445. parse_title(crnt_param, start_token, end_token,
  2446.         xtitle, ytitle, this_plot, do_parse)
  2447. int crnt_param, start_token, end_token;
  2448. char **xtitle, **ytitle;
  2449. struct surface_points *this_plot;
  2450. TBOOLEAN do_parse;
  2451. {
  2452.     static char title[256];
  2453.  
  2454.     if (do_parse) {
  2455.     if (almost_equals(c_token, "t$itle")) {
  2456.         if (parametric) {
  2457.         if (crnt_param)
  2458.             int_error("\"title\" allowed only after parametric function fully specified",
  2459.                   c_token);
  2460.         else {
  2461.             /* Remove default title */
  2462.             if (*xtitle != NULL)
  2463.             (*xtitle)[0] = '\0';
  2464.             if (*ytitle != NULL)
  2465.             (*ytitle)[0] = '\0';
  2466.         }
  2467.         }
  2468.         c_token++;
  2469.         if (isstring(c_token)) {
  2470.         m_quote_capture(&(this_plot->title), c_token, c_token);
  2471.         } else {
  2472.         int_error("expecting \"title\" for plot", c_token);
  2473.         }
  2474.         c_token++;
  2475.     }  else if (almost_equals(c_token, "not$itle")) {
  2476.         c_token++;
  2477.     }  else {
  2478.         m_capture(&(this_plot->title), start_token, end_token);
  2479.         if (crnt_param == 1)
  2480.         *xtitle = this_plot->title;
  2481.         if (crnt_param == 2)
  2482.         *ytitle = this_plot->title;
  2483.     }
  2484.     }
  2485.     else {
  2486.     this_plot->title = alloc(strlen(title) + 1);
  2487.     strcpy(this_plot->title, title);
  2488.     }
  2489. }
  2490.  
  2491. /*
  2492.  * This parses the splot command after any range specifications. To support
  2493.  * autoscaling on the x/z axis, we want any data files to define the x/y
  2494.  * range, then to plot any functions using that range. We thus parse the
  2495.  * input twice, once to pick up the data files, and again to pick up the
  2496.  * functions. Definitions are processed twice, but that won't hurt.
  2497.  */
  2498. eval_3dplots()
  2499. {
  2500.     register int    i, j;
  2501.     register struct surface_points *this_plot=NULL, **tp_3d_ptr;
  2502.     register int    start_token, end_token;
  2503.     register int    begin_token;
  2504.     double          x_min, x_max, y_min, y_max, z_min, z_max;
  2505.     register double x, xdiff, xisodiff, y, ydiff, yisodiff, temp;
  2506.     static struct value a;
  2507.     TBOOLEAN         ltmp, some_data_files = FALSE,is_log_func = FALSE;
  2508.     int             plot_num, line_num, point_num, crnt_param = 0;    /* 0=z, 1=x, 2=y */
  2509.     char           *xtitle;
  2510.     char           *ytitle;
  2511.     void            parametric_3dfixup();
  2512.  
  2513.     /* Reset first_3dplot. This is usually done at the end of this function.
  2514.        If there is an error within this function, the memory is left allocated,
  2515.        since we cannot call sp_free if the list is incomplete */
  2516.     first_3dplot=NULL;
  2517.  
  2518.     if (autoscale_lz) {
  2519.     zmin = VERYLARGE;
  2520.     zmax = -VERYLARGE;
  2521.     } else if (is_log_z && (zmin <= 0.0 || zmax <= 0.0))
  2522.     int_error("z range must be above 0 for log scale!",
  2523.           NO_CARET);
  2524.  
  2525.     tp_3d_ptr = &(first_3dplot);
  2526.     plot_num = 0;
  2527.     line_num = 0;        /* default line type */
  2528.     point_num = 0;        /* default point type */
  2529.  
  2530.     xtitle = NULL;
  2531.     ytitle = NULL;
  2532.  
  2533.     begin_token = c_token;
  2534.  
  2535.     /*** First Pass: Read through data files ***/
  2536.     /*
  2537.      * This pass serves to set the x/yranges and to parse the command, as
  2538.      * well as filling in every thing except the function data. That is done
  2539.      * after the x/yrange is defined.
  2540.      */
  2541.     while (TRUE) {
  2542.     if (END_OF_COMMAND)
  2543.         int_error("function to plt3d expected", c_token);
  2544.  
  2545.     start_token = c_token;
  2546.  
  2547.     if (is_definition(c_token)) {
  2548.         define();
  2549.     } else {
  2550.         plot_num++;
  2551.  
  2552.         if (isstring(c_token)) {    /* data file to plot */
  2553.         int line_type = line_num,
  2554.             point_type = point_num,
  2555.             plot_style = data_style,
  2556.             first_mesh = TRUE;
  2557.  
  2558.         if (parametric && crnt_param != 0)
  2559.             int_error("previous parametric function not fully specified",
  2560.                   c_token);
  2561.  
  2562.         if (!some_data_files) {
  2563.             if (autoscale_lx) {
  2564.             xmin = VERYLARGE;
  2565.             xmax = -VERYLARGE;
  2566.             }
  2567.             if (autoscale_ly) {
  2568.             ymin = VERYLARGE;
  2569.             ymax = -VERYLARGE;
  2570.             }
  2571.         }
  2572.         some_data_files = TRUE;
  2573.  
  2574.         do {
  2575.             if (*tp_3d_ptr)
  2576.             this_plot = *tp_3d_ptr;
  2577.             else {        /* no memory malloc()'d there yet */
  2578.             /* Allocate enough isosamples and samples */
  2579.             this_plot = sp_alloc(0, 0, 0, 0);
  2580.             *tp_3d_ptr = this_plot;
  2581.             }
  2582.  
  2583.             this_plot->plot_type = DATA3D;
  2584.             end_token = c_token;
  2585.             /* this also parses index/using option */
  2586.             get_3ddata(this_plot);
  2587.  
  2588.             parse_title(crnt_param, start_token, end_token,
  2589.                 &xtitle, &ytitle, this_plot, first_mesh);
  2590.             if (!first_mesh) plot_num++;
  2591.  
  2592.             if (first_mesh) {
  2593.             if (almost_equals(c_token, "w$ith")) {
  2594.                 plot_style = this_plot->plot_style = get_style();
  2595.             }
  2596.             if (!equals(c_token, ",") && !END_OF_COMMAND) {
  2597.                 struct value    t;
  2598.                 line_type = (int) real(const_express(&t)) - 1;
  2599.             }
  2600.             if (!equals(c_token, ",") && !END_OF_COMMAND) {
  2601.                 struct value    t;
  2602.                 point_type = (int) real(const_express(&t)) - 1;
  2603.             }
  2604.             first_mesh = FALSE;
  2605.             }
  2606.  
  2607.             this_plot->line_type = line_type;
  2608.             this_plot->point_type = point_type;
  2609.             this_plot->plot_style = plot_style;
  2610.  
  2611.             tp_3d_ptr = &(this_plot->next_sp);
  2612.         }
  2613.         while (more_data_fp);
  2614.         } else {        /* function to plot */
  2615.         if (parametric)    /* Rotate between x/y/z axes */
  2616.             crnt_param = (crnt_param + 1) % 3;
  2617.         if (*tp_3d_ptr) {
  2618.             this_plot = *tp_3d_ptr;
  2619.             if (!hidden3d)
  2620.             sp_replace(this_plot, samples_1, iso_samples_1,
  2621.                                    samples_2, iso_samples_2);
  2622.             else
  2623.             sp_replace(this_plot, iso_samples_1, 0,
  2624.                                    0, iso_samples_2);
  2625.         } else {    /* no memory malloc()'d there yet */
  2626.             /* Allocate enough isosamples and samples */
  2627.             if (!hidden3d)
  2628.             this_plot = sp_alloc(samples_1, iso_samples_1,
  2629.                                              samples_2, iso_samples_2);
  2630.             else
  2631.             this_plot = sp_alloc(iso_samples_1, 0,
  2632.                                              0, iso_samples_2);
  2633.             *tp_3d_ptr = this_plot;
  2634.         }
  2635.  
  2636.         this_plot->plot_type = FUNC3D;
  2637.         this_plot->has_grid_topology = TRUE;
  2638.         this_plot->plot_style = func_style;
  2639.         dummy_func = &plot_func;
  2640.         plot_func.at = temp_at();
  2641.         /* ignore it for now */
  2642.         end_token = c_token - 1;
  2643.  
  2644.         parse_title(crnt_param, start_token, end_token,
  2645.                 &xtitle, &ytitle, this_plot, TRUE);
  2646.  
  2647.         this_plot->line_type = line_num;
  2648.         this_plot->point_type = point_num;
  2649.  
  2650.         if (almost_equals(c_token, "w$ith")) {
  2651.             this_plot->plot_style = get_style();
  2652.         }
  2653.         if (!equals(c_token, ",") && !END_OF_COMMAND) {
  2654.             struct value    t;
  2655.             this_plot->line_type = (int) real(const_express(&t)) - 1;
  2656.         }
  2657.         if (!equals(c_token, ",") && !END_OF_COMMAND) {
  2658.             struct value    t;
  2659.             this_plot->point_type = (int) real(const_express(&t)) - 1;
  2660.         }
  2661.  
  2662.         tp_3d_ptr = &(this_plot->next_sp);
  2663.         }
  2664.  
  2665.         if ((this_plot->plot_style == POINTSTYLE) ||
  2666.         (this_plot->plot_style == LINESPOINTS) ||
  2667.         (this_plot->plot_style == ERRORBARS))
  2668.         if (crnt_param == 0)
  2669.             point_num +=
  2670.             1 + (draw_contour != 0)
  2671.             + (hidden3d != 0);
  2672.         if (crnt_param == 0)
  2673.         line_num += 1 + (draw_contour != 0)
  2674.             + (hidden3d != 0);
  2675.     }
  2676.  
  2677.     if (equals(c_token, ","))
  2678.         c_token++;
  2679.     else
  2680.         break;
  2681.     }
  2682.  
  2683.     if (parametric && crnt_param != 0)
  2684.     int_error("parametric function not fully specified", NO_CARET);
  2685.  
  2686.     if (parametric) {
  2687.     /*
  2688.      * Swap u/v and x/y ranges for duration of these eval_plot
  2689.      * computations.
  2690.      */
  2691.     ltmp = autoscale_lx;
  2692.     autoscale_lx = autoscale_lu;
  2693.     autoscale_lu = ltmp;
  2694.     ltmp = autoscale_ly;
  2695.     autoscale_ly = autoscale_lv;
  2696.     autoscale_lv = ltmp;
  2697.     temp = xmin;
  2698.     xmin = umin;
  2699.     umin = temp;
  2700.     temp = xmax;
  2701.     xmax = umax;
  2702.     umax = temp;
  2703.     temp = ymin;
  2704.     ymin = vmin;
  2705.     vmin = temp;
  2706.     temp = ymax;
  2707.     ymax = vmax;
  2708.     vmax = temp;
  2709.     }
  2710.     /*** Second Pass: Evaluate the functions ***/
  2711.     /*
  2712.      * Everything is defined now, except the function data. We expect no
  2713.      * syntax errors, etc, since the above parsed it all. This makes the code
  2714.      * below simpler. If autoscale_ly, the yrange may still change.
  2715.      */
  2716.     if (xmin == xmax)
  2717.     if (autoscale_lx) {
  2718.         fprintf(stderr, "Warning: empty x range [%g:%g], ",
  2719.             xmin, xmax);
  2720.         if (xmin == 0.0) {
  2721.         /* completely arbitary */
  2722.         xmin = -1.;
  2723.         xmax = 1.;
  2724.         } else {
  2725.         /* expand range by 10% in either direction */
  2726.         xmin = xmin * 0.9;
  2727.         xmax = xmax * 1.1;
  2728.         }
  2729.         fprintf(stderr, "adjusting to [%g:%g]\n", xmin, xmax);
  2730.     } else {
  2731.         int_error("x range is empty", c_token);
  2732.     }
  2733.  
  2734.     if (ymin == ymax)
  2735.     if (autoscale_ly) {
  2736.         fprintf(stderr, "Warning: empty y range [%g:%g], ",
  2737.             ymin, ymax);
  2738.         if (ymin == 0.0) {
  2739.         /* completely arbitary */
  2740.         ymin = -1.;
  2741.         ymax = 1.;
  2742.         } else {
  2743.         /* expand range by 10% in either direction */
  2744.         ymin = ymin * 0.9;
  2745.         ymax = ymax * 1.1;
  2746.         }
  2747.         fprintf(stderr, "adjusting to [%g:%g]\n", ymin, ymax);
  2748.     } else {
  2749.         int_error("y range is empty", c_token);
  2750.     }
  2751.  
  2752.     /* give error if xrange badly set from missing datafile error */
  2753.     if (xmin == VERYLARGE || xmax == -VERYLARGE) {
  2754.     int_error("x range is invalid", c_token);
  2755.     }
  2756.     if (is_log_x) {
  2757.     if (xmin <= 0.0 || xmax <= 0.0)
  2758.         int_error("x range must be greater than 0 for log scale!", NO_CARET);
  2759.     x_min = log(xmin)/log_base_log_x;
  2760.     x_max = log(xmax)/log_base_log_x;
  2761.     } else {
  2762.     x_min = xmin;
  2763.     x_max = xmax;
  2764.     }
  2765.  
  2766.     /* give error if yrange badly set from previous error */
  2767.     if (ymin == VERYLARGE || ymax == -VERYLARGE) {
  2768.     int_error("y range is invalid", c_token);
  2769.     }
  2770.     if (is_log_y) {
  2771.     if (ymin <= 0.0 || ymax <= 0.0)
  2772.         int_error("y range must be greater than 0 for log scale!", NO_CARET);
  2773.     y_min = log(ymin)/log_base_log_y;
  2774.     y_max = log(ymax)/log_base_log_y;
  2775.     } else {
  2776.     y_min = ymin;
  2777.     y_max = ymax;
  2778.     }
  2779.  
  2780.     if (samples_1 < 2 || samples_2 < 2 || iso_samples_1 < 2 || iso_samples_2 < 2)
  2781.     int_error("samples or iso_samples < 2. Must be at least 2.", NO_CARET);
  2782.  
  2783.     if (this_plot && this_plot->has_grid_topology && hidden3d) {
  2784.     xdiff = (x_max - x_min) / (iso_samples_1 - 1);
  2785.     ydiff = (y_max - y_min) / (iso_samples_2 - 1);
  2786.     } else {
  2787.     xdiff = (x_max - x_min) / (samples_1 - 1);
  2788.     ydiff = (y_max - y_min) / (samples_2 - 1);
  2789.     }
  2790.     xisodiff = (x_max - x_min) / (iso_samples_1 - 1);
  2791.     yisodiff = (y_max - y_min) / (iso_samples_2 - 1);
  2792.  
  2793.     this_plot = first_3dplot;
  2794.     c_token = begin_token;    /* start over */
  2795.  
  2796.     /* Read through functions */
  2797.     while (TRUE) {
  2798.     if (is_definition(c_token)) {
  2799.         define();
  2800.     } else {
  2801.         if (isstring(c_token)) {    /* data file to plot */
  2802.         /* ignore this now */
  2803.         c_token++;
  2804.         if (almost_equals(c_token, "i$ndex")) {
  2805.             struct value a;
  2806.             int index;
  2807.  
  2808.             c_token++;        /* skip "index" */
  2809.             index = (int) magnitude(const_express(&a));
  2810.         }
  2811.         if (almost_equals(c_token, "u$sing")) {
  2812.             c_token++;    /* skip "using" */
  2813.             if (!isstring(c_token)) {
  2814.             struct value    a;
  2815.             (void) magnitude(const_express(&a));    /* skip xcol */
  2816.             if (equals(c_token, ":")) {
  2817.                 c_token++;    /* skip ":" */
  2818.                 (void) magnitude(const_express(&a));    /* skip ycol */
  2819.                 if (equals(c_token, ":")) {
  2820.                 c_token++;    /* skip ":" */
  2821.                 (void) magnitude(const_express(&a));    /* skip zcol */
  2822.                 }
  2823.             }
  2824.             }
  2825.             if (isstring(c_token))
  2826.             c_token++;    /* skip format string */
  2827.         }
  2828.         } else {        /* function to plot */
  2829.         struct iso_curve *this_iso = this_plot->iso_crvs;
  2830.         struct coordinate GPHUGE *points = this_iso->points;
  2831.         int num_sam_to_use, num_iso_to_use;
  2832.  
  2833.         if (parametric)
  2834.             crnt_param = (crnt_param + 1) % 3;
  2835.         dummy_func = &plot_func;
  2836.         plot_func.at = temp_at();    /* reparse function */
  2837.  
  2838.         num_iso_to_use = iso_samples_2;
  2839.         if (!(this_plot->has_grid_topology && hidden3d))
  2840.             num_sam_to_use = samples_1;
  2841.         else
  2842.             num_sam_to_use = iso_samples_1;
  2843.  
  2844.         is_log_func=(!parametric)||(crnt_param==0)?is_log_z:(crnt_param==1?is_log_x:is_log_y);
  2845.         for (j = 0; j < num_iso_to_use; j++) {
  2846.             y = y_min + j * yisodiff;
  2847.             /* if (is_log_y) PEM fix logscale y axis */
  2848.             /* y = pow(log_base_log_y,y); 26-Sep-89 */
  2849.             (void) Gcomplex(&plot_func.dummy_values[1],
  2850.                    is_log_y ? pow(base_log_y, y) : y,
  2851.                    0.0);
  2852.  
  2853.             for (i = 0; i < num_sam_to_use; i++) {
  2854.             x = x_min + i * xdiff;
  2855.             /* if (is_log_x) PEM fix logscale x axis */
  2856.             /* x = pow(base_log_x,x); 26-Sep-89 */
  2857.             (void) Gcomplex(&plot_func.dummy_values[0],
  2858.                        is_log_x ? pow(base_log_x, x) : x,
  2859.                        0.0);
  2860.  
  2861.             points[i].x = x;
  2862.             points[i].y = y;
  2863.  
  2864.             evaluate_at(plot_func.at, &a);
  2865.  
  2866.             if (undefined || (fabs(imag(&a)) > zero)) {
  2867.                 points[i].type = UNDEFINED;
  2868.                 continue;
  2869.             }
  2870.             temp = real(&a);
  2871.  
  2872.             if (is_log_func && temp < 0.0) {
  2873.                 points[i].type = UNDEFINED;
  2874.                 continue;
  2875.             }
  2876.             if (is_log_func) {
  2877.                 if (temp == 0.0) {
  2878.                 points[i].type = OUTRANGE;
  2879.                 points[i].z = -VERYLARGE;
  2880.                 continue;
  2881.                 } else {
  2882.                 points[i].z = log(temp)/log_base_log_z;
  2883.                 }
  2884.             } else
  2885.                 points[i].z = temp;
  2886.  
  2887.             if (autoscale_lz || inrange(temp, zmin, zmax)) {
  2888.                 points[i].type = INRANGE;
  2889.                 if (autoscale_lz) {
  2890.                 if (temp < zmin)
  2891.                     zmin = temp;
  2892.                 if (temp > zmax)
  2893.                     zmax = temp;
  2894.                 }
  2895.             } else
  2896.                 points[i].type = OUTRANGE;
  2897.             }
  2898.             this_iso->p_count = num_sam_to_use;
  2899.             this_iso = this_iso->next;
  2900.             points = this_iso? this_iso->points: NULL;
  2901.         }
  2902.  
  2903.         if (!(this_plot->has_grid_topology && hidden3d)) {
  2904.             num_iso_to_use = iso_samples_1;
  2905.             num_sam_to_use = samples_2;
  2906.             is_log_func=(!parametric)||(crnt_param==0)?is_log_z:(crnt_param==1?is_log_x:is_log_y);
  2907.             for (i = 0; i < num_iso_to_use; i++) {
  2908.             x = x_min + i * xisodiff;
  2909.             /* if (is_log_x) PEM fix logscale x axis */
  2910.             /* x = pow(base_log_x,x); 26-Sep-89 */
  2911.             (void) Gcomplex(&plot_func.dummy_values[0],
  2912.                        is_log_x ? pow(base_log_x, x) : x,
  2913.                        0.0);
  2914.  
  2915.             for (j = 0; j < num_sam_to_use; j++) {
  2916.                 y = y_min + j * ydiff;
  2917.                 /* if (is_log_y) PEM fix logscale y axis */
  2918.                 /* y = pow(base_log_y,y); 26-Sep-89 */
  2919.                 (void) Gcomplex(&plot_func.dummy_values[1],
  2920.                        is_log_y ? pow(base_log_y, y) : y,
  2921.                        0.0);
  2922.  
  2923.                 points[j].x = x;
  2924.                 points[j].y = y;
  2925.  
  2926.                 evaluate_at(plot_func.at, &a);
  2927.  
  2928.                 if (undefined || (fabs(imag(&a)) > zero)) {
  2929.                 points[j].type = UNDEFINED;
  2930.                 continue;
  2931.                 }
  2932.                 temp = real(&a);
  2933.  
  2934.                 if (is_log_func && temp < 0.0) {
  2935.                 points[j].type = UNDEFINED;
  2936.                 continue;
  2937.                 }
  2938.                 if (is_log_func) {
  2939.                 if (temp == 0.0) {
  2940.                     points[j].type = OUTRANGE;
  2941.                     points[j].z = -VERYLARGE;
  2942.                     continue;
  2943.                 } else {
  2944.                     points[j].z = log(temp)/log_base_log_z;
  2945.                 }
  2946.                 } else
  2947.                 points[j].z = temp;
  2948.  
  2949.                 if (autoscale_lz
  2950.                 || inrange(temp, zmin, zmax)) {
  2951.                 points[j].type = INRANGE;
  2952.                 if (autoscale_lz) {
  2953.                     if (temp < zmin)
  2954.                     zmin = temp;
  2955.                     if (temp > zmax)
  2956.                     zmax = temp;
  2957.                 }
  2958.                 } else
  2959.                 points[j].type = OUTRANGE;
  2960.             }
  2961.             this_iso->p_count = num_sam_to_use;
  2962.             this_iso = this_iso->next;
  2963.             points = this_iso ? this_iso->points : NULL;
  2964.             }
  2965.         }
  2966.         }
  2967.  
  2968.         /* title was handled above */
  2969.         if (almost_equals(c_token, "t$itle")) {
  2970.         c_token++;
  2971.         c_token++;
  2972.         } else if (almost_equals(c_token, "not$itle")) {
  2973.             c_token++;
  2974.         }
  2975.         /* style was handled above */
  2976.         if (almost_equals(c_token, "w$ith")) {
  2977.         c_token++;
  2978.         c_token++;
  2979.         }
  2980.         /* line and point types were handled above */
  2981.         if (!equals(c_token, ",") && !END_OF_COMMAND) {
  2982.         struct value    t;
  2983.         (void) real(const_express(&t));
  2984.         }
  2985.         if (!equals(c_token, ",") && !END_OF_COMMAND) {
  2986.         struct value    t;
  2987.         (void) real(const_express(&t));
  2988.         }
  2989.         this_plot = this_plot->next_sp;
  2990.     }
  2991.  
  2992.     if (equals(c_token, ","))
  2993.         c_token++;
  2994.     else
  2995.         break;
  2996.     }
  2997.  
  2998.     /* if first_3dplot is NULL, we have no functions or data at all. This can
  2999.        happen, if you type "splot x=5", since x=5 is a variable assignment */
  3000.  
  3001.     if(first_3dplot==NULL) {
  3002.     int_error("no functions or data to plot", c_token);
  3003.     }
  3004.  
  3005.     if (fabs(zmax - zmin) < zero)
  3006.     /* if autoscale, widen range */
  3007.     if (autoscale_lz) {
  3008.         fprintf(stderr, "Warning: empty z range [%g:%g], ", zmin, zmax);
  3009.         if (fabs(zmin) < zero) {
  3010.         zmin = -1.;
  3011.         zmax = 1.;
  3012.         } else {
  3013.         /* expand range by 10% in either direction */
  3014.         zmin = zmin * 0.9;
  3015.         zmax = zmax * 1.1;
  3016.         }
  3017.         fprintf(stderr, "adjusting to [%g:%g]\n", zmin, zmax);
  3018.     } else {
  3019.         int_error("z range is less than `zero`", c_token);
  3020.     }
  3021.  
  3022.     /* Now we finally know the real zmin and zmax */
  3023.     if (is_log_z) {
  3024.     if (zmin <= 0.0 || zmax <= 0.0)
  3025.         int_error("z range must be greater than 0 for log scale!", NO_CARET);
  3026.     z_min = log(zmin)/log_base_log_z;
  3027.     z_max = log(zmax)/log_base_log_z;
  3028.     } else {
  3029.     z_min = zmin;
  3030.     z_max = zmax;
  3031.     }
  3032.  
  3033.     /* Set a flag so capture is not invoked by replot itself. -hmh */
  3034.     if (plot_token != -1) {
  3035.     capture(replot_line, plot_token, c_token);
  3036.     plot_token = -1;
  3037.     }
  3038.  
  3039.     if (parametric) {
  3040.     /* Now put u/v and x/y ranges back before we actually plot anything. */
  3041.     ltmp = autoscale_lx;
  3042.     autoscale_lx = autoscale_lu;
  3043.     autoscale_lu = ltmp;
  3044.     ltmp = autoscale_ly;
  3045.     autoscale_ly = autoscale_lv;
  3046.     autoscale_lv = ltmp;
  3047.     temp = xmin;
  3048.     xmin = umin;
  3049.     umin = temp;
  3050.     temp = xmax;
  3051.     xmax = umax;
  3052.     umax = temp;
  3053.     temp = ymin;
  3054.     ymin = vmin;
  3055.     vmin = temp;
  3056.     temp = ymax;
  3057.     ymax = vmax;
  3058.     vmax = temp;
  3059.  
  3060.     /* Now actually fix the plot triplets to be single plots. */
  3061.     parametric_3dfixup(first_3dplot, &plot_num,
  3062.                &x_min, &x_max, &y_min, &y_max,
  3063.                &z_min, &z_max);
  3064.     if (is_log_x) {
  3065.         if (x_min <= 0.0 || x_max <= 0.0)
  3066.         int_error("x range must be greater than 0 for log scale!", NO_CARET);
  3067.         x_min = log(x_min)/log_base_log_x;
  3068.         x_max = log(x_max)/log_base_log_x;
  3069.     }
  3070.     if (is_log_y) {
  3071.         if (y_min <= 0.0 || y_max <= 0.0)
  3072.         int_error("y range must be greater than 0 for log scale!", NO_CARET);
  3073.         y_min = log(y_min)/log_base_log_y;
  3074.         y_max = log(y_max)/log_base_log_y;
  3075.     }
  3076.     if (is_log_z) {
  3077.         if (z_min <= 0.0 || z_max <= 0.0)
  3078.         int_error("z range must be greater than 0 for log scale!", NO_CARET);
  3079.         z_min = log(z_min)/log_base_log_z;
  3080.         z_max = log(z_max)/log_base_log_z;
  3081.     }
  3082.     }
  3083.  
  3084.     /* Filter out empty meshes. */
  3085.     while (first_3dplot &&
  3086.        first_3dplot->num_iso_read == 0 &&
  3087.        first_3dplot->plot_type == DATA3D) {
  3088.     struct surface_points *plt = first_3dplot->next_sp;
  3089.  
  3090.     first_3dplot->next_sp = NULL;
  3091.     sp_free(first_3dplot);
  3092.     plot_num--;
  3093.     first_3dplot = plt;
  3094.     }
  3095.     if (first_3dplot != NULL) {
  3096.     struct surface_points *plt1, *plt2;
  3097.     
  3098.     for (plt1 = first_3dplot, plt2 = plt1->next_sp; plt2 != NULL; ) {
  3099.         if (plt2->num_iso_read == 0 && plt2->plot_type == DATA3D) {
  3100.         plt2 = plt2->next_sp;
  3101.         plt1->next_sp->next_sp = NULL;
  3102.         sp_free(plt1->next_sp);
  3103.         plot_num--;
  3104.         plt1->next_sp = plt2;
  3105.         }
  3106.         else {
  3107.         plt1 = plt2;
  3108.         plt2 = plt2->next_sp;
  3109.         }
  3110.     }
  3111.     }
  3112.     if (first_3dplot == NULL)
  3113.     int_error("no data found in file", NO_CARET);
  3114.  
  3115.     /* Creates contours if contours are to be plotted as well. */
  3116.     if (draw_contour) {
  3117.     for (this_plot = first_3dplot, i = 0;
  3118.          i < plot_num;
  3119.          this_plot = this_plot->next_sp, i++) {
  3120.         if (this_plot->contours) {
  3121.         struct gnuplot_contours *cntr, *cntrs = this_plot->contours;
  3122.  
  3123.         while (cntrs) {
  3124.             cntr = cntrs;
  3125.             cntrs = cntrs->next;
  3126.             gpfarfree(cntr->coords);
  3127.             free(cntr);
  3128.         }
  3129.         }
  3130.         /* Make sure this one can be contoured. */
  3131.         if (!this_plot->has_grid_topology) {
  3132.         this_plot->contours = NULL;
  3133.         fprintf(stderr,"Notice: cannot contour non grid data!\n");
  3134.         /* changed from int_error by recommendation of rkc@xn.ll.mit.edu */
  3135.         }
  3136.         else if (this_plot->plot_type == DATA3D)
  3137.         this_plot->contours = contour(
  3138.                          this_plot->num_iso_read,
  3139.                          this_plot->iso_crvs,
  3140.                          contour_levels, contour_pts,
  3141.                          contour_kind, contour_order,
  3142.                          levels_kind, levels_list);
  3143.         else
  3144.         this_plot->contours = contour(iso_samples_2,
  3145.                           this_plot->iso_crvs,
  3146.                           contour_levels, contour_pts,
  3147.                           contour_kind, contour_order,
  3148.                           levels_kind, levels_list);
  3149.     }
  3150.     }
  3151.     if (strcmp(term_tbl[term].name, "table") == 0)
  3152.     print_3dtable(plot_num);
  3153.     else
  3154.     do_3dplot(first_3dplot, plot_num, x_min, x_max, y_min, y_max, z_min, z_max);
  3155.     sp_free(first_3dplot);
  3156.     first_3dplot = NULL;
  3157. }
  3158.  
  3159. done(status)
  3160.     int             status;
  3161. {
  3162.     if (term && term_init)
  3163.     (*term_tbl[term].reset) ();
  3164. #ifdef vms
  3165.     vms_reset();
  3166. #endif
  3167.     exit(status);
  3168. }
  3169.  
  3170. void 
  3171. parametric_fixup(start_plot, plot_num, x_min, x_max)
  3172.     struct curve_points *start_plot;
  3173.     int            *plot_num;
  3174.     double         *x_min, *x_max;
  3175. /*
  3176.  * The hardest part of this routine is collapsing the FUNC plot types in the
  3177.  * list (which are gauranteed to occur in (x,y) pairs while preserving the
  3178.  * non-FUNC type plots intact.  This means we have to work our way through
  3179.  * various lists.  Examples (hand checked): start_plot:F1->F2->NULL ==>
  3180.  * F2->NULL start_plot:F1->F2->F3->F4->F5->F6->NULL ==> F2->F4->F6->NULL
  3181.  * start_plot:F1->F2->D1->D2->F3->F4->D3->NULL ==> F2->D1->D2->F4->D3->NULL
  3182.  * 
  3183.  * Of course, the more interesting work is to move the y values of the x
  3184.  * function to become the x values of the y function (checking the mins and
  3185.  * maxs as we go along).
  3186.  */
  3187. {
  3188.     struct curve_points *xp, *new_list, *yp = start_plot, *tmp, *free_list,
  3189.                    *free_head = NULL;
  3190.     int             i, tlen, curve;
  3191.     char           *new_title;
  3192.     double          lxmin, lxmax, temp;
  3193.  
  3194.     if (autoscale_lx) {
  3195.     lxmin = VERYLARGE;
  3196.     lxmax = -VERYLARGE;
  3197.     } else {
  3198.     lxmin = xmin;
  3199.     lxmax = xmax;
  3200.     }
  3201.  
  3202.     /*
  3203.      * Ok, go through all the plots and move FUNC types together.  Note: this
  3204.      * originally was written to look for a NULL next pointer, but gnuplot
  3205.      * wants to be sticky in grabbing memory and the right number of items in
  3206.      * the plot list is controlled by the plot_num variable.
  3207.      * 
  3208.      * Since gnuplot wants to do this sticky business, a free_list of
  3209.      * curve_points is kept and then tagged onto the end of the plot list as
  3210.      * this seems more in the spirit of the original memory behavior than
  3211.      * simply freeing the memory.  I'm personally not convinced this sort of
  3212.      * concern is worth it since the time spent computing points seems to
  3213.      * dominate any garbage collecting that might be saved here...
  3214.      */
  3215.     new_list = xp = start_plot;
  3216.     yp = xp->next_cp;
  3217.     curve = 0;
  3218.  
  3219.     for (; curve < *plot_num; xp = xp->next_cp, yp = yp ? yp->next_cp : yp, curve++) {
  3220.     if (xp->plot_type != FUNC) {
  3221.         continue;
  3222.     }
  3223.     /* Here's a FUNC parametric function defined as two parts. */
  3224.     --(*plot_num);
  3225.     /*
  3226.      * Go through all the points assigning the y's from xp to be the x's
  3227.      * for yp.  Check max's and min's as you go.
  3228.      */
  3229.     for (i = 0; i < yp->p_count; ++i) {
  3230.         /*
  3231.          * Throw away excess xp points, mark excess yp points as
  3232.          * OUTRANGE.
  3233.          */
  3234.         if (i > xp->p_count) {
  3235.         yp->points[i].type = OUTRANGE;
  3236.         continue;
  3237.         }
  3238.         /*
  3239.          * Just as we had to do when we computed y values--now check that
  3240.          * x's (computed parametrically) are in the permitted ranges as
  3241.          * well.
  3242.          */
  3243.         temp = xp->points[i].y;    /* New x value for yp function. */
  3244.         yp->points[i].x = temp;
  3245.         /* Handle undefined values differently from normal ranges. */
  3246.         if (xp->points[i].type == UNDEFINED)
  3247.         yp->points[i].type = xp->points[i].type;
  3248.         if (autoscale_lx || polar
  3249.         || inrange(temp, lxmin, lxmax)) {
  3250.         if (autoscale_lx && temp < lxmin)
  3251.             lxmin = temp;
  3252.         if (autoscale_lx && temp > lxmax)
  3253.             lxmax = temp;
  3254.         } else
  3255.         yp->points[i].type = OUTRANGE;    /* Due to x value. */
  3256.     }
  3257.     /* Ok, fix up the title to include both the xp and yp plots. */
  3258.     if (xp->title && xp->title[0] != '\0') {
  3259.         tlen = strlen(yp->title) + strlen(xp->title) + 3;
  3260.         new_title = alloc((unsigned long) tlen, "string");
  3261.         strcpy(new_title, xp->title);
  3262.         strcat(new_title, ", ");    /* + 2 */
  3263.         strcat(new_title, yp->title);    /* + 1 = + 3 */
  3264.         free(yp->title);
  3265.         yp->title = new_title;
  3266.     }
  3267.     /* Eliminate the first curve (xparam) and just use the second. */
  3268.     if (xp == start_plot) {
  3269.         /* Simply nip off the first element of the list. */
  3270.         new_list = first_plot = yp;
  3271.         xp = xp->next_cp;
  3272.         if (yp->next_cp != NULL)
  3273.         yp = yp->next_cp;
  3274.         /* Add start_plot to the free_list. */
  3275.         if (free_head == NULL) {
  3276.         free_list = free_head = start_plot;
  3277.         free_head->next_cp = NULL;
  3278.         } else {
  3279.         free_list->next_cp = start_plot;
  3280.         start_plot->next_cp = NULL;
  3281.         free_list = start_plot;
  3282.         }
  3283.     } else {
  3284.         /* Here, remove the xp node and replace it with the yp node. */
  3285.         tmp = xp;
  3286.         /* Pass over any data files that might have been in place. */
  3287.         while (new_list->next_cp && new_list->next_cp != xp)
  3288.         new_list = new_list->next_cp;
  3289.         new_list->next_cp = yp;
  3290.         new_list = new_list->next_cp;
  3291.         xp = xp->next_cp;
  3292.         if (yp->next_cp != NULL)
  3293.         yp = yp->next_cp;
  3294.         /* Add tmp to the free_list. */
  3295.         tmp->next_cp = NULL;
  3296.         if (free_head == NULL) {
  3297.         free_list = free_head = tmp;
  3298.         } else {
  3299.         free_list->next_cp = tmp;
  3300.         free_list = tmp;
  3301.         }
  3302.     }
  3303.     }
  3304.     /* Ok, stick the free list at the end of the curve_points plot list. */
  3305.     while (new_list->next_cp != NULL)
  3306.     new_list = new_list->next_cp;
  3307.     new_list->next_cp = free_head;
  3308.  
  3309.     /* Report the overall graph mins and maxs. */
  3310.     *x_min = lxmin;
  3311.     *x_max = lxmax;
  3312. }
  3313.  
  3314. void 
  3315. parametric_3dfixup(start_plot, plot_num, x_min, x_max, y_min, y_max,
  3316.            z_min, z_max)
  3317.     struct surface_points *start_plot;
  3318.     int            *plot_num;
  3319.     double         *x_min, *x_max, *y_min, *y_max, *z_min, *z_max;
  3320. /*
  3321.  * The hardest part of this routine is collapsing the FUNC plot types in the
  3322.  * list (which are gauranteed to occur in (x,y,z) triplets while preserving
  3323.  * the non-FUNC type plots intact.  This means we have to work our way
  3324.  * through various lists.  Examples (hand checked):
  3325.  * start_plot:F1->F2->F3->NULL ==> F3->NULL
  3326.  * start_plot:F1->F2->F3->F4->F5->F6->NULL ==> F3->F6->NULL
  3327.  * start_plot:F1->F2->F3->D1->D2->F4->F5->F6->D3->NULL ==>
  3328.  * F3->D1->D2->F6->D3->NULL
  3329.  */
  3330. {
  3331.     struct surface_points *xp, *yp, *zp, *new_list, *tmp, *free_list, *free_head = NULL;
  3332.     struct iso_curve *icrvs, *xicrvs, *yicrvs, *zicrvs;
  3333.     int             i, tlen, surface;
  3334.     char           *new_title;
  3335.     double          lxmin, lxmax, lymin, lymax, lzmin, lzmax;
  3336.  
  3337.     if (autoscale_lx) {
  3338.     lxmin = VERYLARGE;
  3339.     lxmax = -VERYLARGE;
  3340.     } else {
  3341.     lxmin = xmin;
  3342.     lxmax = xmax;
  3343.     }
  3344.  
  3345.     if (autoscale_ly) {
  3346.     lymin = VERYLARGE;
  3347.     lymax = -VERYLARGE;
  3348.     } else {
  3349.     lymin = ymin;
  3350.     lymax = ymax;
  3351.     }
  3352.  
  3353.     if (autoscale_lz) {
  3354.     lzmin = VERYLARGE;
  3355.     lzmax = -VERYLARGE;
  3356.     } else {
  3357.     lzmin = zmin;
  3358.     lzmax = zmax;
  3359.     }
  3360.  
  3361.     /*
  3362.      * Ok, go through all the plots and move FUNC3D types together.  Note:
  3363.      * this originally was written to look for a NULL next pointer, but
  3364.      * gnuplot wants to be sticky in grabbing memory and the right number of
  3365.      * items in the plot list is controlled by the plot_num variable.
  3366.      * 
  3367.      * Since gnuplot wants to do this sticky business, a free_list of
  3368.      * surface_points is kept and then tagged onto the end of the plot list
  3369.      * as this seems more in the spirit of the original memory behavior than
  3370.      * simply freeing the memory.  I'm personally not convinced this sort of
  3371.      * concern is worth it since the time spent computing points seems to
  3372.      * dominate any garbage collecting that might be saved here...
  3373.      */
  3374.     new_list = xp = start_plot;
  3375.     for (surface = 0; surface < *plot_num; surface++) {
  3376.     if (xp->plot_type != FUNC3D) {
  3377.         icrvs = xp->iso_crvs;
  3378.  
  3379.         while (icrvs) {
  3380.         struct coordinate GPHUGE *points = icrvs->points;
  3381.  
  3382.         for (i = 0; i < icrvs->p_count; ++i) {
  3383.             if (lxmin > points[i].x)
  3384.             lxmin = points[i].x;
  3385.             if (lxmax < points[i].x)
  3386.             lxmax = points[i].x;
  3387.             if (lymin > points[i].y)
  3388.             lymin = points[i].y;
  3389.             if (lymax < points[i].y)
  3390.             lymax = points[i].y;
  3391.             if (lzmin > points[i].z)
  3392.             lzmin = points[i].z;
  3393.             if (lzmax < points[i].z)
  3394.             lzmax = points[i].z;
  3395.         }
  3396.  
  3397.         icrvs = icrvs->next;
  3398.         }
  3399.         xp = xp->next_sp;
  3400.         continue;
  3401.     }
  3402.     yp = xp->next_sp;
  3403.     zp = yp->next_sp;
  3404.  
  3405.     /* Here's a FUNC3D parametric function defined as three parts. */
  3406.     (*plot_num) -= 2;
  3407.     /*
  3408.      * Go through all the points and assign the x's and y's from xp and
  3409.      * yp to zp.  Check max's and min's as you go.
  3410.      */
  3411.     xicrvs = xp->iso_crvs;
  3412.     yicrvs = yp->iso_crvs;
  3413.     zicrvs = zp->iso_crvs;
  3414.     while (zicrvs) {
  3415.         struct coordinate GPHUGE *xpoints = xicrvs->points, GPHUGE *ypoints = yicrvs->points, GPHUGE *zpoints = zicrvs->points;
  3416.         for (i = 0; i < zicrvs->p_count; ++i) {
  3417.         zpoints[i].x = xpoints[i].z;
  3418.         zpoints[i].y = ypoints[i].z;
  3419.  
  3420.         if (lxmin > zpoints[i].x)
  3421.             lxmin = zpoints[i].x;
  3422.         if (lxmax < zpoints[i].x)
  3423.             lxmax = zpoints[i].x;
  3424.         if (lymin > zpoints[i].y)
  3425.             lymin = zpoints[i].y;
  3426.         if (lymax < zpoints[i].y)
  3427.             lymax = zpoints[i].y;
  3428.         if (lzmin > zpoints[i].z)
  3429.             lzmin = zpoints[i].z;
  3430.         if (lzmax < zpoints[i].z)
  3431.             lzmax = zpoints[i].z;
  3432.         }
  3433.         xicrvs = xicrvs->next;
  3434.         yicrvs = yicrvs->next;
  3435.         zicrvs = zicrvs->next;
  3436.     }
  3437.  
  3438.     /* Ok, fix up the title to include xp and yp plots. */
  3439.     if ((xp->title && xp->title[0] != '\0') ||
  3440.         (yp->title && yp->title[0] != '\0')) {
  3441.         tlen = (xp->title ? strlen(xp->title) : 0) +
  3442.         (yp->title ? strlen(yp->title) : 0) +
  3443.         (zp->title ? strlen(zp->title) : 0) + 5;
  3444.         new_title = alloc((unsigned long) tlen, "string");
  3445.         new_title[0] = 0;
  3446.         if (xp->title) {
  3447.         strcat(new_title, xp->title);
  3448.         strcat(new_title, ", ");    /* + 2 */
  3449.         }
  3450.         if (yp->title) {
  3451.         strcat(new_title, yp->title);
  3452.         strcat(new_title, ", ");    /* + 2 */
  3453.         }
  3454.         if (zp->title) {
  3455.         strcat(new_title, zp->title);
  3456.         }
  3457.         free(zp->title);
  3458.         zp->title = new_title;
  3459.     }
  3460.     /*
  3461.      * Eliminate the first two surfaces (xp and yp) and just use the
  3462.      * third.
  3463.      */
  3464.     if (xp == start_plot) {
  3465.         /* Simply nip off the first two elements of the list. */
  3466.         new_list = first_3dplot = zp;
  3467.         xp = zp->next_sp;
  3468.         /* Add xp and yp to the free_list. */
  3469.         if (free_head == NULL) {
  3470.         free_head = start_plot;
  3471.         } else {
  3472.         free_list->next_sp = start_plot;
  3473.         }
  3474.         free_list = start_plot->next_sp;
  3475.         free_list->next_sp = NULL;
  3476.     } else {
  3477.         /*
  3478.          * Here, remove the xp,yp nodes and replace them with the zp
  3479.          * node.
  3480.          */
  3481.         tmp = xp;
  3482.         /* Pass over any data files that might have been in place. */
  3483.         while (new_list->next_sp && new_list->next_sp != xp)
  3484.         new_list = new_list->next_sp;
  3485.         new_list->next_sp = zp;
  3486.         new_list = zp;
  3487.         xp = zp->next_sp;
  3488.         /* Add tmp to the free_list. */
  3489.         if (free_head == NULL) {
  3490.         free_head = tmp;
  3491.         } else {
  3492.         free_list->next_sp = tmp;
  3493.         }
  3494.         free_list = tmp->next_sp;
  3495.         free_list->next_sp = NULL;
  3496.     }
  3497.     }
  3498.     /* Ok, stick the free list at the end of the surface_points plot list. */
  3499.     while (new_list->next_sp != NULL)
  3500.     new_list = new_list->next_sp;
  3501.     new_list->next_sp = free_head;
  3502.     if (lxmax - lxmin < zero) {
  3503.     if (fabs(lxmax) < zero) {
  3504.         lxmin = -1.0;
  3505.         lxmax = 1.0;
  3506.     } else {
  3507.         lxmin *= 0.9;
  3508.         lxmax *= 1.1;
  3509.     }
  3510.     }
  3511.     if (lymax - lymin < zero) {
  3512.     if (fabs(lymax) < zero) {
  3513.         lymin = -1.0;
  3514.         lymax = 1.0;
  3515.     } else {
  3516.         lymin *= 0.9;
  3517.         lymax *= 1.1;
  3518.     }
  3519.     }
  3520.     if (lzmax - lzmin < zero) {
  3521.     if (fabs(lzmax) < zero) {
  3522.         lzmin = -1.0;
  3523.         lzmax = 1.0;
  3524.     } else {
  3525.         lzmin *= 0.9;
  3526.         lzmax *= 1.1;
  3527.     }
  3528.     }
  3529.     /* Report the overall graph mins and maxs. */
  3530.     if (autoscale_lx) {
  3531.     *x_min = (is_log_x ? pow(base_log_x, lxmin) : lxmin);
  3532.     *x_max = (is_log_x ? pow(base_log_x, lxmax) : lxmax);
  3533.     } else {
  3534.     *x_min = xmin;
  3535.     *x_max = xmax;
  3536.     }
  3537.     if (autoscale_ly) {
  3538.     *y_min = (is_log_y ? pow(base_log_y, lymin) : lymin);
  3539.     *y_max = (is_log_y ? pow(base_log_y, lymax) : lymax);
  3540.     } else {
  3541.     *y_min = ymin;
  3542.     *y_max = ymax;
  3543.     }
  3544.     if (autoscale_lz) {
  3545.     *z_min = (is_log_z ? pow(base_log_z, lzmin) : lzmin);
  3546.     *z_max = (is_log_z ? pow(base_log_z, lzmax) : lzmax);
  3547.     } else {
  3548.     *z_min = zmin;
  3549.     *z_max = zmax;
  3550.     }
  3551. }
  3552.  
  3553. #ifdef AMIGA_SC_6_1
  3554. void 
  3555. sleep(delay)
  3556.     unsigned int    delay;
  3557. {
  3558.     Delay(50 * delay);
  3559. }
  3560. #endif
  3561.  
  3562. #ifdef AMIGA_AC_5
  3563. void 
  3564. sleep(delay)
  3565.     unsigned int    delay;
  3566. {
  3567.     unsigned long   time_is_up;
  3568.     time_is_up = time(NULL) + (unsigned long) delay;
  3569.     while (time(NULL) < time_is_up)
  3570.      /* wait */ ;
  3571. }
  3572. #endif
  3573.  
  3574. #if defined(MSDOS) || defined(_Windows) || defined(DOS386)
  3575. #if (!defined(__TURBOC__) && !defined(__EMX__) && !defined(DJGPP)) || defined(_Windows) /* Turbo C already has sleep() */
  3576. #ifndef __ZTC__            /* ZTC already has usleep() */
  3577. /* kludge to provide sleep() for msc 5.1 */
  3578. void 
  3579. sleep(delay)
  3580.     unsigned int    delay;
  3581. {
  3582.     unsigned long   time_is_up;
  3583.     time_is_up = time(NULL) + (unsigned long) delay;
  3584.     while (time(NULL) < time_is_up)
  3585.      /* wait */ ;
  3586. }
  3587. #endif                /* not ZTC */
  3588. #endif                /* (!TURBOC && !__EMX__ && !DJGPP) or _Windows */
  3589. #endif                /* MSDOS || _Windows*/
  3590.  
  3591.  
  3592. /* Support for input, shell, and help for various systems */
  3593.  
  3594. #ifdef vms
  3595.  
  3596. #include <descrip.h>
  3597. #include <rmsdef.h>
  3598. #include <errno.h>
  3599. #include <smgdef.h>
  3600. #include <smgmsg.h>
  3601.  
  3602. extern          lib$get_input(), lib$put_output();
  3603. extern          smg$read_composed_line();
  3604.  
  3605. int             vms_len;
  3606.  
  3607. unsigned int    status[2] =
  3608. {1, 0};
  3609.  
  3610. static char     help[MAX_LINE_LEN + 1] = "gnuplot";
  3611.  
  3612. $DESCRIPTOR(prompt_desc, PROMPT);
  3613. $DESCRIPTOR(line_desc, input_line);
  3614.  
  3615. $DESCRIPTOR(help_desc, help);
  3616. $DESCRIPTOR(helpfile_desc, "GNUPLOT$HELP");
  3617.  
  3618.  
  3619. read_line(prompt)
  3620.     char           *prompt;
  3621. {
  3622.     int             more, start = 0;
  3623.     char            expand_prompt[40];
  3624.  
  3625.     prompt_desc.dsc$w_length = strlen(prompt);
  3626.     prompt_desc.dsc$a_pointer = prompt;
  3627.     (void) strcpy(expand_prompt, "_");
  3628.     (void) strncat(expand_prompt, prompt, 38);
  3629.     do {
  3630.     line_desc.dsc$w_length = MAX_LINE_LEN - start;
  3631.     line_desc.dsc$a_pointer = &input_line[start];
  3632.     switch (status[1] = smg$read_composed_line(&vms_vkid, 0, &line_desc, &prompt_desc, &vms_len)) {
  3633.     case SMG$_EOF:
  3634.         done(IO_SUCCESS);    /* ^Z isn't really an error */
  3635.         break;
  3636.     case RMS$_TNS:        /* didn't press return in time */
  3637.         vms_len--;        /* skip the last character */
  3638.         break;        /* and parse anyway */
  3639.     case RMS$_BES:        /* Bad Escape Sequence */
  3640.     case RMS$_PES:        /* Partial Escape Sequence */
  3641.         sys$putmsg(status);
  3642.         vms_len = 0;    /* ignore the line */
  3643.         break;
  3644.     case SS$_NORMAL:
  3645.         break;        /* everything's fine */
  3646.     default:
  3647.         done(status[1]);    /* give the error message */
  3648.     }
  3649.     start += vms_len;
  3650.     input_line[start] = '\0';
  3651.     inline_num++;
  3652.     if (input_line[start - 1] == '\\') {
  3653.         /* Allow for a continuation line. */
  3654.         prompt_desc.dsc$w_length = strlen(expand_prompt);
  3655.         prompt_desc.dsc$a_pointer = expand_prompt;
  3656.         more = 1;
  3657.         --start;
  3658.     } else {
  3659.         line_desc.dsc$w_length = strlen(input_line);
  3660.         line_desc.dsc$a_pointer = input_line;
  3661.         more = 0;
  3662.     }
  3663.     } while (more);
  3664.     return 0;
  3665. }
  3666.  
  3667.  
  3668. do_help()
  3669. {
  3670.     help_desc.dsc$w_length = strlen(help);
  3671.     if ((vaxc$errno = lbr$output_help(lib$put_output, 0, &help_desc,
  3672.                &helpfile_desc, 0, lib$get_input)) != SS$_NORMAL)
  3673.     os_error("can't open GNUPLOT$HELP", NO_CARET);
  3674. }
  3675.  
  3676.  
  3677. do_shell()
  3678. {
  3679.     if ((vaxc$errno = lib$spawn()) != SS$_NORMAL) {
  3680.     os_error("spawn error", NO_CARET);
  3681.     }
  3682. }
  3683.  
  3684.  
  3685. do_system()
  3686. {
  3687.     input_line[0] = ' ';    /* an embarrassment, but... */
  3688.  
  3689.     if ((vaxc$errno = lib$spawn(&line_desc)) != SS$_NORMAL)
  3690.     os_error("spawn error", NO_CARET);
  3691.  
  3692.     (void) putc('\n', stderr);
  3693. }
  3694.  
  3695. #else                /* vms */
  3696.  
  3697. #ifdef _Windows
  3698. do_help()
  3699. {
  3700.     if (END_OF_COMMAND)
  3701.         WinHelp(textwin.hWndParent,(LPSTR)winhelpname,HELP_INDEX,(DWORD)NULL);
  3702.     else {
  3703.         char buf[128];
  3704.         int start = c_token++;
  3705.         while (!(END_OF_COMMAND))
  3706.             c_token++;
  3707.         capture(buf, start, c_token-1);
  3708.         WinHelp(textwin.hWndParent,(LPSTR)winhelpname,HELP_PARTIALKEY,(DWORD)buf);
  3709.     }
  3710. }
  3711. #else
  3712.  
  3713. /*
  3714.  * do_help: (not VMS, although it would work) Give help to the user. It
  3715.  * parses the command line into helpbuf and supplies help for that string.
  3716.  * Then, if there are subtopics available for that key, it prompts the user
  3717.  * with this string. If more input is given, do_help is called recursively,
  3718.  * with the argument the index of null character in the string. Thus a more
  3719.  * specific help can be supplied. This can be done repeatedly. If null input
  3720.  * is given, the function returns, effecting a backward climb up the tree.
  3721.  * David Kotz (David.Kotz@Dartmouth.edu) 10/89
  3722.  */
  3723.  
  3724. do_help()
  3725. {
  3726.     static char    *helpbuf = NULL;
  3727.     static char    *prompt = NULL;
  3728.     int             base;    /* index of first char AFTER help string */
  3729.     int             len;    /* length of current help string */
  3730.     TBOOLEAN         more_help;
  3731.     TBOOLEAN         only;    /* TRUE if only printing subtopics */
  3732.     int             subtopics;    /* 0 if no subtopics for this topic */
  3733.     int             start;    /* starting token of help string */
  3734.     char           *help_ptr;    /* name of help file */
  3735. #ifdef ATARI
  3736.     static char    help_fname[256]=""; /* keep helpfilename across calls */
  3737. #endif
  3738.  
  3739.     if ((help_ptr = getenv("GNUHELP")) == (char *) NULL)
  3740. #ifndef ATARI
  3741.     /* if can't find environment variable then just use HELPFILE */
  3742.     help_ptr = HELPFILE;
  3743. #else
  3744.     /* try whether we can find the helpfile via shell_find. If not, just
  3745.        use the default. (tnx Andreas) */
  3746.  
  3747. #ifdef sequent
  3748.     if( !index( HELPFILE, ':' ) && !index( HELPFILE, '/' ) &&
  3749.         !index( HELPFILE, '\\' ) ) {
  3750. #else
  3751.     if( !strchr( HELPFILE, ':' ) && !strchr( HELPFILE, '/' ) &&
  3752.         !strchr( HELPFILE, '\\' ) ) {
  3753. #endif
  3754.         if( strlen(help_fname)==0 ) {
  3755.         strcpy( help_fname, HELPFILE );
  3756.         if( shel_find( help_fname )==0 ) {
  3757.             strcpy( help_fname, HELPFILE );
  3758.         }
  3759.         }
  3760.         help_ptr=help_fname;
  3761.     } else {
  3762.         help_ptr=HELPFILE;
  3763.     }
  3764. #endif /* ATARI */
  3765.  
  3766.     /* Since MSDOS DGROUP segment is being overflowed we can not allow such  */
  3767.     /* huge static variables (1k each). Instead we dynamically allocate them */
  3768.     /* on the first call to this function...                     */
  3769.     if (helpbuf == NULL) {
  3770.     helpbuf = alloc((unsigned long)MAX_LINE_LEN, "help buffer");
  3771.     prompt = alloc((unsigned long)MAX_LINE_LEN, "help prompt");
  3772.     helpbuf[0] = prompt[0] = 0;
  3773.     }
  3774.     len = base = strlen(helpbuf);
  3775.  
  3776.     /* find the end of the help command */
  3777.     for (start = c_token; !(END_OF_COMMAND); c_token++);
  3778.     /* copy new help input into helpbuf */
  3779.     if (len > 0)
  3780.     helpbuf[len++] = ' ';    /* add a space */
  3781.     capture(helpbuf + len, start, c_token - 1);
  3782.     squash_spaces(helpbuf + base);    /* only bother with new stuff */
  3783.     lower_case(helpbuf + base);    /* only bother with new stuff */
  3784.     len = strlen(helpbuf);
  3785.  
  3786.     /* now, a lone ? will print subtopics only */
  3787.     if (strcmp(helpbuf + (base ? base + 1 : 0), "?") == 0) {
  3788.     /* subtopics only */
  3789.     subtopics = 1;
  3790.     only = TRUE;
  3791.     helpbuf[base] = '\0';    /* cut off question mark */
  3792.     } else {
  3793.     /* normal help request */
  3794.     subtopics = 0;
  3795.     only = FALSE;
  3796.     }
  3797.  
  3798.     switch (help(helpbuf, help_ptr, &subtopics)) {
  3799.     case H_FOUND:{
  3800.         /* already printed the help info */
  3801.         /* subtopics now is true if there were any subtopics */
  3802.         screen_ok = FALSE;
  3803.  
  3804.         do {
  3805.         if (subtopics && !only) {
  3806.             /* prompt for subtopic with current help string */
  3807.             if (len > 0)
  3808.             (void) sprintf(prompt, "Subtopic of %s: ", helpbuf);
  3809.             else
  3810.             (void) strcpy(prompt, "Help topic: ");
  3811.             read_line(prompt);
  3812.             num_tokens = scanner(input_line);
  3813.             c_token = 0;
  3814.             more_help = !(END_OF_COMMAND);
  3815.             if (more_help)
  3816.             /* base for next level is all of current helpbuf */
  3817.             do_help();
  3818.         } else
  3819.             more_help = FALSE;
  3820.         } while (more_help);
  3821.  
  3822.         break;
  3823.     }
  3824.     case H_NOTFOUND:{
  3825.         printf("Sorry, no help for '%s'\n", helpbuf);
  3826.         break;
  3827.     }
  3828.     case H_ERROR:{
  3829.         perror(help_ptr);
  3830.         break;
  3831.     }
  3832.     default:{            /* defensive programming */
  3833.         int_error("Impossible case in switch", NO_CARET);
  3834.         /* NOTREACHED */
  3835.     }
  3836.     }
  3837.  
  3838.     helpbuf[base] = '\0';    /* cut it off where we started */
  3839. }
  3840. #endif  /* _Windows */
  3841.  
  3842. #ifdef AMIGA_AC_5
  3843. char            strg0[256];
  3844. #endif
  3845.  
  3846. do_system()
  3847. {
  3848. #ifdef AMIGA_AC_5
  3849.     char           *parms[80];
  3850.     void            getparms();
  3851.  
  3852.     getparms(input_line + 1, parms);
  3853.     if (fexecv(parms[0], parms) < 0)
  3854. #else
  3855. #if defined(ATARI)&&defined(__GNUC__)
  3856.     /* use preloaded shell, if available */
  3857.     short           (*shell_p) (char *command);
  3858.     void           *ssp;
  3859.  
  3860.     ssp = (void *) Super(NULL);
  3861.     shell_p = *(short (**) (char *)) 0x4f6;
  3862.     Super(ssp);
  3863.  
  3864.     /* this is a bit strange, but we have to have a single if */
  3865.     if ((shell_p ? (*shell_p) (input_line + 1) : system(input_line + 1)))
  3866. #else
  3867. #ifdef _Windows
  3868.     if (winsystem(input_line + 1))
  3869. #else
  3870.     if (system(input_line + 1))
  3871. #endif
  3872. #endif
  3873. #endif
  3874.     os_error("system() failed", NO_CARET);
  3875. }
  3876.  
  3877. #ifdef AMIGA_AC_5
  3878.  
  3879. /******************************************************************************/
  3880. /* */
  3881. /* Parses the command string (for fexecv use) and  converts the first token  */
  3882. /* to lower case                                                          */
  3883. /* */
  3884. /******************************************************************************/
  3885.  
  3886. void 
  3887. getparms(command, parms)
  3888.     char           *command;
  3889.     char          **parms;
  3890. {
  3891.     register int    i = 0;    /* A bunch of indices          */
  3892.     register int    j = 0;
  3893.     register int    k = 0;
  3894.  
  3895.     while (*(command + j) != '\0') {    /* Loop on string characters   */
  3896.     parms[k++] = strg0 + i;
  3897.     while (*(command + j) == ' ')
  3898.         ++j;
  3899.     while (*(command + j) != ' ' && *(command + j) != '\0') {
  3900.         if (*(command + j) == '"')    /* Get quoted string           */
  3901.         for (*(strg0 + (i++)) = *(command + (j++));
  3902.              *(command + j) != '"';
  3903.              *(strg0 + (i++)) = *(command + (j++)));
  3904.         *(strg0 + (i++)) = *(command + (j++));
  3905.     }
  3906.     *(strg0 + (i++)) = '\0';/* NUL terminate every token   */
  3907.     }
  3908.     parms[k] = '\0';
  3909.  
  3910.     for (k = strlen(strg0) - 1; k >= 0; --k)    /* Convert to lower case       */
  3911.     *(strg0 + k) >= 'A' && *(strg0 + k) <= 'Z' ? *(strg0 + k) |= 32 : *(strg0 + k);
  3912. }
  3913.  
  3914. #endif                /* AMIGA_AC_5 */
  3915.  
  3916. #ifdef READLINE
  3917. char           *
  3918. rlgets(s, n, prompt)
  3919.     char           *s;
  3920.     int             n;
  3921.     char           *prompt;
  3922. {
  3923.     char           *readline();
  3924.     static char    *line = (char *) NULL;
  3925.  
  3926.     /* If we already have a line, first free it */
  3927.     if (line != (char *) NULL)
  3928.     free(line);
  3929.  
  3930.     line = readline((interactive) ? prompt : "");
  3931.  
  3932.     /* If it's not an EOF */
  3933.     if (line) {
  3934.     if (*line)
  3935.         add_history(line);
  3936.     strncpy(s, line, n);
  3937.     return s;
  3938.     }
  3939.     return line;
  3940. }
  3941. #endif                /* READLINE */
  3942.  
  3943. #if defined(MSDOS) || defined(_Windows) || defined(DOS386)
  3944.  
  3945. #ifndef _Windows
  3946. #ifdef __TURBOC__ 
  3947. /* cgets implemented using dos functions */
  3948. /* Maurice Castro 22/5/91 */
  3949. char           *
  3950. doscgets(s)
  3951.     char           *s;
  3952. {
  3953.     long            datseg;
  3954.  
  3955.     /* protect and preserve segments - call dos to do the dirty work */
  3956.     datseg = _DS;
  3957.  
  3958.     _DX = FP_OFF(s);
  3959.     _DS = FP_SEG(s);
  3960.     _AH = 0x0A;
  3961.     geninterrupt(33);
  3962.     _DS = datseg;
  3963.  
  3964.     /* check for a carriage return and then clobber it with a null */
  3965.     if (s[s[1] + 2] == '\r')
  3966.     s[s[1] + 2] = 0;
  3967.  
  3968.     /* return the input string */
  3969.     return (&(s[2]));
  3970. }
  3971. #endif                /* __TURBOC__ */
  3972. #endif                /* !_Windows */
  3973.  
  3974. #ifdef __ZTC__
  3975. void cputs(char *s)
  3976. {
  3977.    register int i = 0;
  3978.    while (s[i] != '\0')  bdos(0x02, s[i++], NULL);
  3979. }
  3980. char *cgets(char *s)
  3981. {
  3982.    bdosx(0x0A, s, NULL);
  3983.  
  3984.    if (s[s[1]+2] == '\r')
  3985.       s[s[1]+2] = 0;
  3986.  
  3987.    /* return the input string */
  3988.    return(&(s[2]));
  3989. }
  3990. #endif   /* __ZTC__ */
  3991.  
  3992.  
  3993. read_line(prompt)
  3994.     char           *prompt;
  3995. {
  3996.     register int    i;
  3997.     int             start = 0, ilen = 0;
  3998.     TBOOLEAN         more;
  3999.     int             last;
  4000.     char           *p, *crnt_prompt = prompt;
  4001.  
  4002.     if (interactive) {        /* if interactive use console IO so CED will
  4003.                  * work */
  4004. #ifndef READLINE
  4005. #if defined(_Windows) || defined(__EMX__) || defined(DJGPP) || defined(__ZTC__)
  4006.     printf("%s", prompt);
  4007. #else
  4008.     cputs(prompt);
  4009. #endif
  4010. #endif                /* READLINE */
  4011.     do {
  4012.         ilen = MAX_LINE_LEN - start - 1;
  4013.         input_line[start] = ilen > 126 ? 126 : ilen;
  4014. #ifdef READLINE
  4015.         input_line[start + 2] = 0;
  4016.         (void) rlgets(&(input_line[start + 2]), ilen, crnt_prompt);
  4017. #ifdef sequent
  4018.         if ((p = index(&(input_line[start + 2]), '\r'))!=NULL)
  4019.         *p = 0;
  4020.         if ((p = index(&(input_line[start + 2]), '\n'))!=NULL)
  4021.         *p = 0;
  4022. #else
  4023.         if ((p = strchr(&(input_line[start + 2]), '\r'))!=NULL)
  4024.         *p = 0;
  4025.         if ((p = strchr(&(input_line[start + 2]), '\n'))!=NULL)
  4026.         *p = 0;
  4027. #endif
  4028.         input_line[start + 1] = strlen(&(input_line[start + 2]));
  4029. #else                /* READLINE */
  4030. #if defined(_Windows) || defined(__EMX__) || defined(DJGPP)
  4031.         (void) gets(&(input_line[start+2]));
  4032. #else
  4033. #ifdef __TURBOC__
  4034.         (void) doscgets(&(input_line[start]));
  4035. #else                /* __TURBOC__ */
  4036.         (void) cgets(&(input_line[start]));
  4037. #endif                /* __TURBOC__ */
  4038. #endif                /* _Windows || __EMX__ || DJGPP*/
  4039.         (void) putc('\n', stderr);
  4040. #endif                /* READLINE */
  4041.         if (input_line[start + 2] == 26) {
  4042.         /* end-of-file */
  4043.         (void) putc('\n', stderr);
  4044.         input_line[start] = '\0';
  4045.         inline_num++;
  4046.         if (start > 0)    /* don't quit yet - process what we have */
  4047.             more = FALSE;
  4048.         else {
  4049.             (void) putc('\n', stderr);
  4050.             return(1); /* exit gnuplot */
  4051.             /* NOTREACHED */
  4052.         }
  4053.         } else {
  4054.         /* normal line input */
  4055.         register        i = start;
  4056.         while ((input_line[i] = input_line[i + 2]) != (char) NULL)
  4057.             i++;    /* yuck!  move everything down two characters */
  4058.  
  4059.         inline_num++;
  4060.         last = strlen(input_line) - 1;
  4061.         if (last<0) last=0;  /* stop UAE in Windows */
  4062.         if (last + 1 >= MAX_LINE_LEN)
  4063.             int_error("Input line too long", NO_CARET);
  4064.  
  4065.         if (input_line[last] == '\\') {    /* line continuation */
  4066.             start = last;
  4067.             more = TRUE;
  4068.         } else
  4069.             more = FALSE;
  4070.         }
  4071. #ifndef READLINE
  4072.         if (more)
  4073. #if defined(_Windows) || defined(__EMX__) || defined(DJGPP) || defined(__ZTC__)
  4074.         printf("> ");
  4075. #else
  4076.         cputs("> ");
  4077. #endif
  4078. #else
  4079.         crnt_prompt = "> ";
  4080. #endif                /* READLINE */
  4081.     } while (more);
  4082.     } else {            /* not interactive */
  4083.     if (interactive)
  4084.         fputs(prompt, stderr);
  4085.     do {
  4086.         /* grab some input */
  4087.         if (fgets(&(input_line[start]), MAX_LINE_LEN - start, stdin)
  4088.         == (char *) NULL) {
  4089.         /* end-of-file */
  4090.         if (interactive)
  4091.             (void) putc('\n', stderr);
  4092.         input_line[start] = '\0';
  4093.         inline_num++;
  4094.         if (start > 0)    /* don't quit yet - process what we have */
  4095.             more = FALSE;
  4096.         else
  4097.             return(1);  /* exit gnuplot */
  4098.         } else {
  4099.         /* normal line input */
  4100.         last = strlen(input_line) - 1;
  4101.         if (input_line[last] == '\n') {    /* remove any newline */
  4102.             input_line[last] = '\0';
  4103.             /* Watch out that we don't backup beyond 0 (1-1-1) */
  4104.             if (last > 0)
  4105.             --last;
  4106.             inline_num++;
  4107.         } else if (last + 1 >= MAX_LINE_LEN)
  4108.             int_error("Input line too long", NO_CARET);
  4109.  
  4110.         if (input_line[last] == '\\') {    /* line continuation */
  4111.             start = last;
  4112.             more = TRUE;
  4113.         } else
  4114.             more = FALSE;
  4115.         }
  4116.         if (more && interactive)
  4117.         fputs("> ", stderr);
  4118.     } while (more);
  4119.     }
  4120.     return(0);
  4121. }
  4122.  
  4123.  
  4124. do_shell()
  4125. {
  4126.     register char  *comspec;
  4127.     if ((comspec = getenv("COMSPEC")) == (char *) NULL)
  4128.     comspec = "\\command.com";
  4129. #ifdef _Windows
  4130.     if (WinExec(comspec, SW_SHOWNORMAL) <= 32)
  4131. #else
  4132. #ifdef DJGPP
  4133.     if (system(comspec) == -1)
  4134. #else
  4135.     if (spawnl(P_WAIT, comspec, NULL) == -1)
  4136. #endif
  4137. #endif
  4138.     os_error("unable to spawn shell", NO_CARET);
  4139. }
  4140.  
  4141. #else                /* MSDOS */
  4142. /* plain old Unix */
  4143.  
  4144. read_line(prompt)
  4145.     char           *prompt;
  4146. {
  4147.     int             start = 0;
  4148.     TBOOLEAN         more = FALSE;
  4149.     int             last = 0;
  4150.  
  4151. #ifndef READLINE
  4152.     if (interactive)
  4153.     fputs(prompt, stderr);
  4154. #endif                /* READLINE */
  4155.     do {
  4156.     /* grab some input */
  4157. #ifdef READLINE
  4158.     if (((interactive)
  4159.          ? rlgets(&(input_line[start]), MAX_LINE_LEN - start,
  4160.               ((more) ? "> " : prompt))
  4161.          : fgets(&(input_line[start]), MAX_LINE_LEN - start, stdin))
  4162.         == (char *) NULL) {
  4163. #else
  4164.     if (fgets(&(input_line[start]), MAX_LINE_LEN - start, stdin)
  4165.         == (char *) NULL) {
  4166. #endif                /* READLINE */
  4167.         /* end-of-file */
  4168.         if (interactive)
  4169.         (void) putc('\n', stderr);
  4170.         input_line[start] = '\0';
  4171.         inline_num++;
  4172.         if (start > 0)    /* don't quit yet - process what we have */
  4173.         more = FALSE;
  4174.         else
  4175.         return(1); /* exit gnuplot */
  4176.     } else {
  4177.         /* normal line input */
  4178.         last = strlen(input_line) - 1;
  4179.         if (input_line[last] == '\n') {    /* remove any newline */
  4180.         input_line[last] = '\0';
  4181.         /* Watch out that we don't backup beyond 0 (1-1-1) */
  4182.         if (last > 0)
  4183.             --last;
  4184.         } else if (last + 1 >= MAX_LINE_LEN)
  4185.         int_error("Input line too long", NO_CARET);
  4186.  
  4187.         if (input_line[last] == '\\') {    /* line continuation */
  4188.         start = last;
  4189.         more = TRUE;
  4190.         } else
  4191.         more = FALSE;
  4192.     }
  4193. #ifndef READLINE
  4194.     if (more && interactive)
  4195.         fputs("> ", stderr);
  4196. #endif
  4197.     } while (more);
  4198.     return(0);
  4199. }
  4200.  
  4201. #ifdef VFORK
  4202.  
  4203. do_shell()
  4204. {
  4205.     register char  *shell;
  4206.     register int    p;
  4207.     static int      execstat;
  4208.     if (!(shell = getenv("SHELL")))
  4209.     shell = SHELL;
  4210. #ifdef AMIGA_AC_5
  4211.     execstat = fexecl(shell, shell, NULL);
  4212. #else
  4213.     if ((p = vfork()) == 0) {
  4214.     execstat = execl(shell, shell, NULL);
  4215.     _exit(1);
  4216.     } else if (p == -1)
  4217.     os_error("vfork failed", c_token);
  4218.     else
  4219.     while (wait(NULL) != p)
  4220. #endif
  4221.         ;
  4222.     if (execstat == -1)
  4223.     os_error("shell exec failed", c_token);
  4224.     (void) putc('\n', stderr);
  4225. }
  4226. #else                /* VFORK */
  4227.  
  4228. #ifdef AMIGA_SC_6_1
  4229. do_shell()
  4230. {
  4231.     register char  *shell;
  4232.     if (!(shell = getenv("SHELL")))
  4233.     shell = SHELL;
  4234.  
  4235.     if (system(shell))
  4236.     os_error("system() failed", NO_CARET);
  4237.  
  4238.     (void) putc('\n', stderr);
  4239. }
  4240. #else                /* AMIGA_SC_6_1 */
  4241. #ifdef OS2
  4242. do_shell()
  4243. {
  4244.     register char  *shell;
  4245.     if (!(shell = getenv("COMSPEC")))
  4246.     shell = SHELL;
  4247.  
  4248.     if (system(shell) == -1 )
  4249.     os_error("system() failed", NO_CARET);
  4250.  
  4251.     (void) putc('\n', stderr);
  4252. }
  4253. #else                           /* ! OS2 */
  4254. #define EXEC "exec "
  4255. do_shell()
  4256. {
  4257.     static char     exec[100] = EXEC;
  4258.     register char  *shell;
  4259.     if (!(shell = getenv("SHELL")))
  4260.     shell = SHELL;
  4261.  
  4262.     if (system(strncpy(&exec[sizeof(EXEC) - 1], shell,
  4263.                sizeof(exec) - sizeof(EXEC) - 1)))
  4264.     os_error("system() failed", NO_CARET);
  4265.  
  4266.     (void) putc('\n', stderr);
  4267. }
  4268. #endif                          /* OS2 */
  4269. #endif                /* AMIGA_SC_6_1 */
  4270. #endif                /* VFORK */
  4271. #endif                /* MSDOS */
  4272. #endif                /* vms */
  4273.  
  4274. #ifdef _Windows
  4275. /* there is a system like call on MS Windows but it is a bit difficult to 
  4276.    use, so we will invoke the command interpreter and use it to execute the 
  4277.    commands */
  4278. int winsystem(s)
  4279. char *s;
  4280. {
  4281.     LPSTR comspec;
  4282.     LPSTR execstr;
  4283.     LPSTR p;
  4284.  
  4285.     /* get COMSPEC environment variable */
  4286. #ifdef WIN32
  4287.     char envbuf[81];
  4288.     GetEnvironmentVariable("COMSPEC", envbuf, 80);
  4289.     if (*envbuf == '\0')
  4290.         comspec = "\\command.com";
  4291.     else
  4292.         comspec = envbuf;
  4293. #else
  4294.     p = GetDOSEnvironment();
  4295.     comspec = "\\command.com";
  4296.     while (*p) {
  4297.         if (!strncmp(p,"COMSPEC=",8)) {
  4298.             comspec=p+8;
  4299.             break;
  4300.         }
  4301.         p+=strlen(p)+1;
  4302.     }
  4303. #endif
  4304.     /* if the command is blank we must use command.com */
  4305.     p = s;
  4306.     while ((*p == ' ') || (*p == '\n') || (*p == '\r'))
  4307.         p++;
  4308.     if (*p == '\0')
  4309.     {
  4310.         WinExec(comspec, SW_SHOWNORMAL);
  4311.         }
  4312.     else
  4313.     {
  4314.         /* attempt to run the windows/dos program via windows */
  4315.         if (WinExec(s, SW_SHOWNORMAL) <= 32)
  4316.         {
  4317.             /* attempt to run it as a dos program from command line */
  4318.             execstr = (char *) malloc(strlen(s) + strlen(comspec) + 6);
  4319.             strcpy(execstr, comspec);
  4320.             strcat(execstr, " /c ");
  4321.             strcat(execstr, s);
  4322.             WinExec(execstr, SW_SHOWNORMAL);
  4323.             free(execstr);
  4324.             }
  4325.         }
  4326.  
  4327.     /* regardless of the reality return OK - the consequences of */
  4328.     /* failure include shutting down Windows */
  4329.     return(0);        /* success */
  4330.     }
  4331. #endif
  4332.